- All Superinterfaces:
AutoCloseable, Wrapper
A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connection.
A Connection
object's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. This information is obtained with the getMetaData
method.
Note: When configuring a Connection
, JDBC applications should use the appropriate Connection
method such as setAutoCommit
or setTransactionIsolation
. Applications should not invoke SQL commands directly to change the connection's configuration when there is a JDBC method available. By default, a Connection
object is in auto-commit mode, which means that it automatically commits changes after executing each statement. If auto-commit mode has been disabled, the method commit
must be called explicitly in order to commit changes; otherwise, database changes will not be saved.
A new Connection
object created using the JDBC 2.1 core API has an initially empty type map associated with it. A user may enter a custom mapping for a UDT in this type map. When a UDT is retrieved from a data source with the method ResultSet.getObject
, the getObject
method will check the connection's type map to see if there is an entry for that UDT. If so, the getObject
method will map the UDT to the class indicated. If there is no entry, the UDT will be mapped using the standard mapping.
A user may create a new type map, which is a java.util.Map
object, make an entry in it, and pass it to the java.sql
methods that can perform custom mapping. In this case, the method will use the given type map instead of the one associated with the connection.
For example, the following code fragment specifies that the SQL type ATHLETES
will be mapped to the class Athletes
in the Java programming language. The code fragment retrieves the type map for the Connection
object con
, inserts the entry into it, and then sets the type map with the new entry as the connection's type map.
java.util.Map map = con.getTypeMap();
map.put("mySchemaName.ATHLETES", Class.forName("Athletes"));
con.setTypeMap(map);
- Since:
- 1.1
- See Also:
Fields
- =public static final int TRANSACTION_NONE = 0
- =public static final int TRANSACTION_READ_COMMITTED = 2
- =public static final int TRANSACTION_READ_UNCOMMITTED = 1
- =public static final int TRANSACTION_REPEATABLE_READ = 4
- =public static final int TRANSACTION_SERIALIZABLE = 8
Methods
- =public abstract void abort(java.util.concurrent.Executor arg0) throws java.sql.SQLException
- =public default void beginRequest() throws java.sql.SQLException
- =public abstract void clearWarnings() throws java.sql.SQLException
- =public abstract void close() throws java.sql.SQLException
- =public abstract void commit() throws java.sql.SQLException
- =public abstract java.sql.Array createArrayOf(java.lang.String arg0, java.lang.Object[] arg1) throws java.sql.SQLException
- =public abstract java.sql.Blob createBlob() throws java.sql.SQLException
- =public abstract java.sql.Clob createClob() throws java.sql.SQLException
- =public abstract java.sql.NClob createNClob() throws java.sql.SQLException
- =public abstract java.sql.SQLXML createSQLXML() throws java.sql.SQLException
- =public abstract java.sql.Statement createStatement() throws java.sql.SQLException
- =public abstract java.sql.Statement createStatement(int arg0, int arg1) throws java.sql.SQLException
- =public abstract java.sql.Statement createStatement(int arg0, int arg1, int arg2) throws java.sql.SQLException
- =public abstract java.sql.Struct createStruct(java.lang.String arg0, java.lang.Object[] arg1) throws java.sql.SQLException
- =public default void endRequest() throws java.sql.SQLException
- +Only in: JDBC 4.5; not in: JDBC 4.4.public default java.lang.String enquoteIdentifier(java.lang.String arg0, boolean arg1) throws java.sql.SQLExceptionNot in JDBC 4.4; only in JDBC 4.5
enquoteIdentifier
Returns asimple SQL identifieror a delimited identifier. A delimited identifier represents the name of a database object such as a table, column, or view that is enclosed by a delimiter, which is typically a double quote as defined by the SQL standard.If
identifieris a simple SQL identifier:- If
alwaysDelimitisfalse, return the original value - if
alwaysDelimitistrue, enquote the original value and return as a delimited identifier
identifieris not a simple SQL identifier, the delimitedidentifierto be returned must be enclosed by the delimiter returned fromDatabaseMetaData.getIdentifierQuoteString(). If the datasource does not support delimited identifiers, aSQLFeatureNotSupportedExceptionis thrown.A
SQLExceptionwill be thrown ifidentifiercontains any invalid characters within a delimited identifier or the identifier length is invalid for the datasource.- Implementation Requirements:
- The default implementation uses the following criteria to
determine a valid simple SQL identifier:
- The string is not enclosed in double quotes
- The first character is an alphabetic character from a (
'\0061') through z ('\u007A'), or from A ('\u0041') through Z ('\u005A') - The name only contains alphanumeric characters([0-9A-Za-z]) or the character "_"
SQLExceptionif:-
DatabaseMetaData.getIdentifierQuoteString()does not return a double quote identifiercontains anullcharacter or double quote- The length of
identifieris less than 1 or greater than 128 characters
Examples of the conversion: identifier alwaysDelimit Result Hello false Hello Hello true "Hello" G'Day false "G'Day" "Bruce Wayne" false "Bruce Wayne" "Bruce Wayne" true "Bruce Wayne" "select" false "select" "select" true "select" GoodDay$ false "GoodDay$" Hello"World false SQLException "Hello"World" false SQLException - Implementation Note:
- JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.
- Parameters:
identifier- a SQL identifieralwaysDelimit- indicates if a simple SQL identifier should be returned as a delimited identifier- Returns:
- A simple SQL identifier or a delimited identifier
- Throws:
SQLException- if identifier is not a valid identifierSQLFeatureNotSupportedException- if the datasource does not support delimited identifiersNullPointerException- if identifier isnull- Since:
- 26
- If
- +Only in: JDBC 4.5; not in: JDBC 4.4.public default java.lang.String enquoteLiteral(java.lang.String arg0) throws java.sql.SQLExceptionNot in JDBC 4.4; only in JDBC 4.5
enquoteLiteral
Returns aStringenclosed in single quotes. Any occurrence of a single quote within the string will be replaced by two single quotes.Examples of the conversion: Value Result Hello 'Hello' G'Day 'G''Day' 'G''Day' '''G''''Day''' I'''M 'I''''''M' - Implementation Requirements:
- The default implementation creates the literal as:
"'" + val.replace("'", "''") + "'". - Implementation Note:
- JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.
- Parameters:
val- a character string- Returns:
- A string enclosed by single quotes with every single quote converted to two single quotes
- Throws:
NullPointerException- if val isnullSQLException- if a database access error occurs- Since:
- 26
- +Only in: JDBC 4.5; not in: JDBC 4.4.public default java.lang.String enquoteNCharLiteral(java.lang.String arg0) throws java.sql.SQLExceptionNot in JDBC 4.4; only in JDBC 4.5
enquoteNCharLiteral
Returns aStringrepresenting a National Character Set Literal enclosed in single quotes and prefixed with a upper case letter N. Any occurrence of a single quote within the string will be replaced by two single quotes.Examples of the conversion: Value Result Hello N'Hello' G'Day N'G''Day' 'G''Day' N'''G''''Day''' I'''M N'I''''''M' N'Hello' N'N''Hello''' - Implementation Requirements:
- The default implementation creates the literal as:
"N'" + val.replace("'", "''") + "'". - Implementation Note:
- JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource. An implementation of enquoteNCharLiteral may accept a different set of characters than that accepted by the same drivers implementation of enquoteLiteral.
- Parameters:
val- a character string- Returns:
- the result of replacing every single quote character in the argument by two single quote characters where this entire result is then prefixed with 'N'.
- Throws:
NullPointerException- if val isnullSQLException- if a database access error occurs- Since:
- 26
- =public abstract boolean getAutoCommit() throws java.sql.SQLException
- =public abstract java.lang.String getCatalog() throws java.sql.SQLException
- =public abstract java.util.Properties getClientInfo() throws java.sql.SQLException
- =public abstract java.lang.String getClientInfo(java.lang.String arg0) throws java.sql.SQLException
- =public abstract int getHoldability() throws java.sql.SQLException
- =public abstract java.sql.DatabaseMetaData getMetaData() throws java.sql.SQLException
- =public abstract int getNetworkTimeout() throws java.sql.SQLException
- =public abstract java.lang.String getSchema() throws java.sql.SQLException
- =public abstract int getTransactionIsolation() throws java.sql.SQLException
- =public abstract java.util.Map<java.lang.String, java.lang.Class<?>> getTypeMap() throws java.sql.SQLException
- =public abstract java.sql.SQLWarning getWarnings() throws java.sql.SQLException
- =public abstract boolean isClosed() throws java.sql.SQLException
- =public abstract boolean isReadOnly() throws java.sql.SQLException
- +Only in: JDBC 4.5; not in: JDBC 4.4.public default boolean isSimpleIdentifier(java.lang.String arg0) throws java.sql.SQLExceptionNot in JDBC 4.4; only in JDBC 4.5
isSimpleIdentifier
Returns whetheridentifieris a simple SQL identifier. A simple SQL identifier is referred to as regular (or ordinary) identifier within the SQL standard. A regular identifier represents the name of a database object such as a table, column, or view.The rules for a regular Identifier are:
- The first character is an alphabetic character from a (
'\u0061') through z ('\u007A'), or from A ('\u0041') through Z ('\u005A') - The name only contains alphanumeric characters([0-9A-Za-z]) or the character "_"
- It cannot be a SQL reserved word
A datasource may have additional rules for a regular identifier such as:
- Supports additional characters within the name based on the locale being used
- Supports a different maximum length for the identifier
- Implementation Requirements:
- The default implementation uses the following criteria to
determine a valid simple SQL identifier:
- The identifier is not enclosed in double quotes
- The first character is an alphabetic character from a through z, or from A through Z
- The identifier only contains alphanumeric characters([0-9A-Za-z]) or the character "_"
- The identifier is not a SQL reserved word
- The identifier is between 1 and 128 characters in length inclusive
Examples of the conversion: identifier Simple Identifier Hello true G'Day false "Bruce Wayne" false GoodDay$ false Hello"World false "Hello"World" false "select" false "from" false - Implementation Note:
- JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.
- Parameters:
identifier- a SQL identifier- Returns:
- true if a simple SQL identifier, false otherwise
- Throws:
NullPointerException- if identifier isnullSQLException- if a database access error occurs- Since:
- 26
- The first character is an alphabetic character from a (
- =public abstract boolean isValid(int arg0) throws java.sql.SQLException
- =public abstract java.lang.String nativeSQL(java.lang.String arg0) throws java.sql.SQLException
- =public abstract java.sql.CallableStatement prepareCall(java.lang.String arg0) throws java.sql.SQLException
- =public abstract java.sql.CallableStatement prepareCall(java.lang.String arg0, int arg1, int arg2) throws java.sql.SQLException
- =public abstract java.sql.CallableStatement prepareCall(java.lang.String arg0, int arg1, int arg2, int arg3) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0, int arg1) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0, int arg1, int arg2) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0, int arg1, int arg2, int arg3) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0, int[] arg1) throws java.sql.SQLException
- =public abstract java.sql.PreparedStatement prepareStatement(java.lang.String arg0, java.lang.String[] arg1) throws java.sql.SQLException
- =public abstract void releaseSavepoint(java.sql.Savepoint arg0) throws java.sql.SQLException
- =public abstract void rollback() throws java.sql.SQLException
- =public abstract void rollback(java.sql.Savepoint arg0) throws java.sql.SQLException
- =public abstract void setAutoCommit(boolean arg0) throws java.sql.SQLException
- =public abstract void setCatalog(java.lang.String arg0) throws java.sql.SQLException
- =public abstract void setClientInfo(java.lang.String arg0, java.lang.String arg1) throws java.sql.SQLClientInfoException
- =public abstract void setClientInfo(java.util.Properties arg0) throws java.sql.SQLClientInfoException
- =public abstract void setHoldability(int arg0) throws java.sql.SQLException
- =public abstract void setNetworkTimeout(java.util.concurrent.Executor arg0, int arg1) throws java.sql.SQLException
- =public abstract void setReadOnly(boolean arg0) throws java.sql.SQLException
- =public abstract java.sql.Savepoint setSavepoint() throws java.sql.SQLException
- =public abstract java.sql.Savepoint setSavepoint(java.lang.String arg0) throws java.sql.SQLException
- =public abstract void setSchema(java.lang.String arg0) throws java.sql.SQLException
- =public default void setShardingKey(java.sql.ShardingKey arg0) throws java.sql.SQLException
- =public default void setShardingKey(java.sql.ShardingKey arg0, java.sql.ShardingKey arg1) throws java.sql.SQLException
- =public default boolean setShardingKeyIfValid(java.sql.ShardingKey arg0, int arg1) throws java.sql.SQLException
- =public default boolean setShardingKeyIfValid(java.sql.ShardingKey arg0, java.sql.ShardingKey arg1, int arg2) throws java.sql.SQLException
- =public abstract void setTransactionIsolation(int arg0) throws java.sql.SQLException
- =public abstract void setTypeMap(java.util.Map<java.lang.String, java.lang.Class<?>> arg0) throws java.sql.SQLException
Summary
| Elements | Comments | Descriptions | Total | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed | ||
| Connection | 1 | 1 | 1 | 3 | ||||||
| enquoteIdentifier(String,boolean) | 1 | 1 | 2 | |||||||
| enquoteLiteral(String) | 1 | 1 | 2 | |||||||
| enquoteNCharLiteral(String) | 1 | 1 | 2 | |||||||
| isSimpleIdentifier(String) | 1 | 1 | 2 | |||||||
| Total | 4 | 5 | 1 | 1 | 11 | |||||