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);
DriverManager#getConnection(String), Statement, ResultSet, DatabaseMetaData
| Modifier and Type | Field and Description |
|---|---|
| public static final int | TRANSACTION_NONE
A constant indicating that transactions are not supported. |
| public static final int | TRANSACTION_READ_COMMITTED
A constant indicating that dirty reads are prevented; non-repeatable reads and phantom reads can occur. |
| public static final int | TRANSACTION_READ_UNCOMMITTED
A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur. |
| public static final int | TRANSACTION_REPEATABLE_READ
A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur. |
| public static final int | TRANSACTION_SERIALIZABLE
A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public default void | beginRequest()
Hints to the driver that a request, an independent unit of work, is beginning on this connection. |
| public void | |
| public void | close()
Redeclares java. Releases this |
| public void | commit()
Makes all changes made since the previous
commit/rollback permanent and releases any database locks
currently held by this |
| public Array | Returns: an Array object whose elements map to the specified SQL typethe SQL name of the type the elements of the array map to. The typeName is a
database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This
is the value returned by typeName, Object[] Array.getBaseTypeNamethe elements that populate the returned object elements)Factory method for creating Array objects. |
| public Blob | Returns: An object that implements theBlob interfaceConstructs an object that implements the |
| public Clob | Returns: An object that implements theClob interfaceConstructs an object that implements the |
| public NClob | Returns: An object that implements theNClob interfaceConstructs an object that implements the |
| public SQLXML | Returns: An object that implements theSQLXML interfaceConstructs an object that implements the |
| public Statement | Returns: a new defaultStatement objectCreates a |
| public Statement | Returns: a newStatement object that will generate
ResultSet objects with the given type and
concurrencya result set type; one of
resultSetType, int ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEa concurrency type; one of
resultSetConcurrency)ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLECreates a |
| public Statement | Returns: a newStatement object that will generate
ResultSet objects with the given type,
concurrency, and holdabilityone of the following resultSetType, int ResultSet
constants:
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEone of the following resultSetConcurrency, int ResultSet
constants:
ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLEone of the following resultSetHoldability)ResultSet
constants:
ResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMITCreates a |
| public Struct | Returns: a Struct object that maps to the given SQL type and is populated with the given attributesthe SQL type name of the SQL structured type that this typeName, Object[] Struct
object maps to. The typeName is the name of a user-defined type that
has been defined for this database. It is the value returned by
Struct.getSQLTypeName.the attributes that populate the returned object attributes)Factory method for creating Struct objects. |
| public default void | |
| public default String | Returns: A simple SQL identifier or a delimited identifiera SQL identifier identifier, boolean indicates if a simple SQL identifier should be
returned as a delimited identifier alwaysDelimit)Returns a |
| public default String | Returns: A string enclosed by single quotes with every single quote converted to two single quotesa character string val)Returns a |
| public default 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'.a character string val)Returns a |
| public boolean | Returns: the current state of thisConnection object's
auto-commit modeRetrieves the current auto-commit mode for this |
| public String | Returns: the current catalog name ornull if there is noneRetrieves this |
| public String | Returns: The value of the client info property specifiedThe name of the client info property to retrieve name)Returns the value of the client info property specified by name. |
| public Properties | Returns: AProperties object that contains the name and current value of
each of the client info properties supported by the driver.Returns a list containing the name and current value of each client info property supported by the driver. |
| public int | Returns: the holdability, one ofResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMITRetrieves the current holdability of |
| public DatabaseMetaData | Returns: aDatabaseMetaData object for this
Connection objectRetrieves a |
| public int | Returns: the current timeout limit in milliseconds; zero means there is no limitRetrieves the number of milliseconds the driver will wait for a database request to complete. |
| public String | Returns: the current schema name ornull if there is noneRetrieves this |
| public int | Returns: the current transaction isolation level, which will be one of the following constants:Connection.TRANSACTION_READ_UNCOMMITTED,
Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_REPEATABLE_READ,
Connection.TRANSACTION_SERIALIZABLE, or
Connection.TRANSACTION_NONE.Retrieves this |
| public Map | Returns: thejava.util.Map object associated
with this Connection objectRetrieves the |
| public SQLWarning | Returns: the firstSQLWarning object or null
if there are noneRetrieves the first warning reported by calls on this
|
| public boolean | Returns: true if this Connection object
is closed; false if it is still openRetrieves whether this |
| public boolean | Returns: true if this Connection object
is read-only; false otherwiseRetrieves whether this |
| public default boolean | Returns: true if a simple SQL identifier, false otherwisea SQL identifier identifier)Returns whether |
| public boolean | Returns: true if the connection is valid, false otherwiseThe time in seconds to wait for the database operation
used to validate the connection to complete. If the
timeout period expires before the operationcompletes,
this method returns false. A value of 0 indicates a
timeout is not applied to the database operation. timeout)Returns true if the connection has not been closed and is still valid. |
| public String | |
| public CallableStatement | Returns: a new defaultCallableStatement object containing the
pre-compiled SQL statementan SQL statement that may contain one or more '?'
parameter placeholders. Typically this statement is specified using JDBC
call escape syntax. sql)Creates a |
| public CallableStatement | Returns: a newCallableStatement object containing the
pre-compiled SQL statement that will produce ResultSet
objects with the given type and concurrencya sql, int String object that is the SQL statement to
be sent to the database; may contain on or more '?' parametersa result set type; one of
resultSetType, int ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEa concurrency type; one of
resultSetConcurrency)ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLECreates a |
| public CallableStatement | Returns: a newCallableStatement object, containing the
pre-compiled SQL statement, that will generate
ResultSet objects with the given type,
concurrency, and holdabilitya sql, int String object that is the SQL statement to
be sent to the database; may contain on or more '?' parametersone of the following resultSetType, int ResultSet
constants:
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEone of the following resultSetConcurrency, int ResultSet
constants:
ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLEone of the following resultSetHoldability)ResultSet
constants:
ResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMITCreates a |
| public PreparedStatement | Returns: a new defaultPreparedStatement object containing the
pre-compiled SQL statementan SQL statement that may contain one or more '?' IN
parameter placeholders sql)Creates a |
| public PreparedStatement | Returns: a new PreparedStatement object containing the pre-compiled SQL statement that will produceResultSet
objects with the given type and concurrencya sql, int String object that is the SQL statement to
be sent to the database; may contain one or more '?' IN
parametersa result set type; one of
resultSetType, int ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEa concurrency type; one of
resultSetConcurrency)ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLECreates a |
| public PreparedStatement | Returns: a newPreparedStatement object, containing the
pre-compiled SQL statement, that will generate
ResultSet objects with the given type,
concurrency, and holdabilitya sql, int String object that is the SQL statement to
be sent to the database; may contain one or more '?' IN
parametersone of the following resultSetType, int ResultSet
constants:
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEone of the following resultSetConcurrency, int ResultSet
constants:
ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLEone of the following resultSetHoldability)ResultSet
constants:
ResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMITCreates a |
| public PreparedStatement | Returns: a newPreparedStatement object, containing the
pre-compiled SQL statement, that will have the capability of
returning auto-generated keysan SQL statement that may contain one or more '?' IN
parameter placeholders sql, int a flag indicating whether auto-generated keys
should be returned; one of
autoGeneratedKeys)Statement.RETURN_GENERATED_KEYS or
Statement.NO_GENERATED_KEYSCreates a default |
| public PreparedStatement | Returns: a newPreparedStatement object, containing the
pre-compiled statement, that is capable of returning the
auto-generated keys designated by the given array of column
indexesan SQL statement that may contain one or more '?' IN
parameter placeholders sql, int[] an array of column indexes indicating the columns
that should be returned from the inserted row or rows columnIndexes)Creates a default |
| public PreparedStatement | Returns: a newPreparedStatement object, containing the
pre-compiled statement, that is capable of returning the
auto-generated keys designated by the given array of column
namesan SQL statement that may contain one or more '?' IN
parameter placeholders sql, String[] an array of column names indicating the columns
that should be returned from the inserted row or rows columnNames)Creates a default |
| public void | releaseSavepoint(Savepoint
the savepoint)Savepoint object to be removedRemoves the specified |
| public void | rollback()
Undoes all changes made in the current transaction
and releases any database locks currently held
by this |
| public void | |
| public void | setAutoCommit(boolean
true to enable auto-commit mode;
false to disable itSets this connection's auto-commit mode to the given state. |
| public void | setCatalog(String
the name of a catalog (subspace in this
catalog)Connection object's database) in which to workSets the given catalog name in order to select
a subspace of this |
| public void | setClientInfo(String
The name of the client info property to set name, String The value to set the client info property to. If the
value is null, the current value of the specified
property is cleared. value)Sets the value of the client info property specified by name to the value specified by value. |
| public void | setClientInfo(Properties
the list of client info properties to set properties)Sets the value of the connection's client info properties. |
| public void | setHoldability(int
a holdability)ResultSet holdability constant; one of
ResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMITChanges the default holdability of |
| public void | setNetworkTimeout(Executor
The executor, int Executor implementation which will
be used by setNetworkTimeout.The time in milliseconds to wait for the database
operation
to complete. If the JDBC driver does not support milliseconds, the
JDBC driver will round the value up to the nearest second. If the
timeout period expires before the operation
completes, a SQLException will be thrown.
A value of 0 indicates that there is not timeout for database operations. milliseconds)Sets the maximum period a |
| public void | setReadOnly(boolean
true enables read-only mode;
false disables itPuts this connection in read-only mode as a hint to the driver to enable database optimizations. |
| public Savepoint | Returns: the newSavepoint objectCreates an unnamed savepoint in the current transaction and
returns the new |
| public Savepoint | Returns: the newSavepoint objecta name)String containing the name of the savepointCreates a savepoint with the given name in the current transaction
and returns the new |
| public void | |
| public default void | setShardingKey(ShardingKey
the sharding key to set on this connection. The sharding
key may be shardingKey, ShardingKey nullthe super sharding key to set on this connection.
The super sharding key may be superShardingKey)nullSpecifies a shardingKey and superShardingKey to use with this Connection |
| public default void | setShardingKey(ShardingKey
the sharding key to set on this connection. The sharding
key may be shardingKey)nullSpecifies a shardingKey to use with this Connection |
| public default boolean | Returns: true if the connection is valid and the sharding keys are valid and set on this connection; false if the sharding keys are not valid or the timeout period expires before the operation completes.the sharding key to be validated against this connection.
The sharding key may be shardingKey, ShardingKey nullthe super sharding key to be validated against this
connection. The super sharding key may be superShardingKey, int null.time in seconds before which the validation process is expected to
be completed, otherwise the validation process is aborted. A value of 0 indicates
the validation process will not time out. timeout)Sets and validates the sharding keys for this connection. |
| public default boolean | Returns: true if the connection is valid and the sharding key is valid to be set on this connection; false if the sharding key is not valid or the timeout period expires before the operation completes.the sharding key to be validated against this connection.
The sharding key may be shardingKey, int nulltime in seconds before which the validation process is expected to
be completed,else the validation process is aborted. A value of 0 indicates
the validation process will not time out. timeout)Sets and validates the sharding key for this connection. |
| public void | setTransactionIsolation(int
one of the following level)Connection constants:
Connection.TRANSACTION_READ_UNCOMMITTED,
Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_REPEATABLE_READ, or
Connection.TRANSACTION_SERIALIZABLE.
(Note that Connection.TRANSACTION_NONE cannot be used
because it specifies that transactions are not supported.)Attempts to change the transaction isolation level for this
|
| public void | setTypeMap(Map<String, Class<?>>
the map)java.util.Map object to install
as the replacement for this Connection
object's default type mapInstalls the given |