Updated on 2024-08-20 GMT+08:00

java.sql.Connection

This section describes java.sql.Connection, the API for connecting to a database.

Table 1 Support status for java.sql.Connection

Method Name

Description

Return Type

throws

JDBC4 Supported or Not

abort(Executor executor)

Aborts an open connection.

void

SQLException

Yes

clearWarnings()

Clears all warnings reported for this connection object.

void

SQLException

Yes

close()

Releases the database and JDBC resources of this connection object immediately instead of waiting for them to be automatically released.

void

SQLException

Yes

commit()

Makes all changes made since the last commit/rollback permanent and releases any database locks currently held by this connection object. This method should be used only when the autocommit mode is disabled.

void

SQLException

Yes

createArrayOf(String typeName, Object[] elements)

Creates an array object using this factory method.

Array

SQLException

Yes

createBlob()

Constructs an object that implements the BLOB interface. The returned object does not contain data initially. The setBinaryStream and setBytes methods of the BLOB interface can be used to add data to BLOBs.

Blob

SQLException

Yes

createClob()

Constructs an object that implements the CLOB interface. The returned object does not contain data initially. The setAsciiStream, setCharacterStream, and setString methods of the CLOB interface can be used to add data to CLOBs.

Clob

SQLException

Yes

createSQLXML()

Constructs an object that implements the SQLXML interface. The returned object does not contain data initially. You can use the createXmlStreamWriter object of the SQLXML interface and the setString method to add data to an SQLXML object.

SQLXML

SQLException

Yes

createStatement()

Creates a statement object that sends SQL statements to a database.

Statement

SQLException

Yes

createStatement(int resultSetType, int resultSetConcurrency)

Creates a statement object that will generate a ResultSet object with a given type and concurrency.

Statement

SQLException

Yes

createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)

Creates a statement object that will generate a ResultSet object with a given type and concurrency.

Statement

SQLException

Yes

getAutoCommit()

Retrieves the current autocommit mode for this connection object.

boolean

SQLException

Yes

getCatalog()

Obtains the current directory name of this connection object.

String

SQLException

Yes

getClientInfo()

Returns a list containing the name and current value of each client information property supported by the driver. If the client information property has not been set and has no default value, the value of this attribute may be NULL.

Properties

SQLException

Yes

getClientInfo(String name)

Returns the value of the client information property specified by name. This method may return NULL if the specified client information property has not been set and there is no default value. This method also returns NULL if the driver does not support the specified client information property name.

String

SQLException

Yes

getHoldability()

Obtains the current holdability of the ResultSet object created using this connection object.

int

SQLException

Yes

getMetaData()

Retrieves the DatabaseMetaData object that contains metadata about the database to which this connection object represents the connection. Metadata includes information about database tables, supported SQL syntax, stored procedures, and functions of the connection.

DatabaseMetaData

SQLException

Yes

getNetworkTimeout()

Obtains the number of milliseconds the driver will wait for a database request to complete. If the limit is exceeded, SQLException is thrown.

int

SQLException

Yes

getSchema()

Retrieves the current schema name of this connection object.

String

SQLException

Yes

getTransactionIsolation()

Retrieves the current transaction isolation level of this connection object.

int

SQLException

Yes

getTypeMap()

Retrieves the map object associated with this connection object. Unless the application adds an entry, the returned type mapping will be empty.

Map<String,Class<?>>

SQLException

Yes

getWarnings()

Retrieves the first warning reported by calls on this connection object. If there are multiple warnings, subsequent warnings are linked to the first warning and can be retrieved by calling the SQLWarning.getNextWarning method on the previously retrieved warning.

SQLWarning

SQLException

Yes

isClosed()

Checks whether this connection object has been closed. If a method has been called to close the connection, or if some major error occurs, the connection is closed. The value true is returned only when this method is called after the close ction.Close method is called.

boolean

SQLException

Yes

isReadOnly()

Checks whether this connection object is in read-only mode.

boolean

SQLException

Yes

isValid(int timeout)

Returns true if the connection has not been closed and is still valid. The driver should commit a query to the connection, or use other mechanisms to affirmatively verify that the connection is still valid when this method is called.

Boolean

SQLException

Yes

nativeSQL(String sql)

Converts a given SQL statement to the native SQL syntax of the system. The driver can convert the JDBC SQL syntax to the native SQL syntax of its system before sending it. This method returns the native form of the statement that the driver should have sent.

String

SQLException

Yes

prepareCall(String sql)

Creates a CallableStatement object that calls a database stored procedure. The CallableStatement object provides methods for setting its IN and OUT parameters, as well as methods for executing calls to stored procedures.

CallableStatement

SQLException

Yes

prepareCall(String sql, int resultSetType, int resultSetConcurrency)

Creates a CallableStatement object. This object will generate a ResultSet object with the given type and concurrency. This method is the same as the preparation call method above, but it allows overwriting the default result set type and concurrency.

CallableStatement

SQLException

Yes

prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)

Creates a CallableStatement object. This object will generate a ResultSet object with the given type and concurrency. This method is the same as the preparation call method above, but it allows overwriting the default result set type, result set concurrency type, and holdability.

CallableStatement

SQLException

Yes

prepareStatement(String sql)

Creates a prepared statement object for sending parameterized SQL statements to a database.

PreparedStatement

SQLException

Yes

prepareStatement(String sql, int autoGeneratedKeys)

Creates the default prepared statement object that can retrieve automatically generated keys. The given constant tells the driver whether it should make the automatically generated key available for retrieval. If the SQL statement is not an INSERT statement or an SQL statement that can return an automatically generated key, this parameter is ignored.

PreparedStatement

SQLException

Yes

prepareStatement(String sql, int[] columnIndexes)

Creates the default prepared statement object that can return an automatically generated key specified by a given array. This array contains the indexes of the columns in the target table that contain the automatically generated keys, which should be available. If the SQL statement is not an INSERT statement or an SQL statement that can return an automatically generated key, the driver ignores the array.

PreparedStatement

SQLException

Yes

prepareStatement(String sql, int resultSetType, int resultSetConcurrency)

Creates a prepared statement object that will generate a ResultSet object with a given type and concurrency. This method is the same as the prepared statement method above, but it allows overwriting the default result set type and concurrency.

PreparedStatement

SQLException

Yes

prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)

Creates a prepared statement object that will generate a ResultSet object with a given type, concurrency, and holdability.

PreparedStatement

SQLException

Yes

prepareStatement(String sql, String[] columnNames)

Creates the default prepared statement object that can return an automatically generated key specified by a given array. This array contains the names of the columns in the target table that contain the automatically generated keys, which should be returned. If the SQL statement is not an INSERT statement or an SQL statement that can return an automatically generated key, the driver ignores the array.

PreparedStatement

SQLException

Yes

releaseSavepoint(Savepoint savepoint)

Deletes the specified savepoint and subsequent savepoint objects from the current transaction. Any reference to the savepoint after it is deleted will cause an SQL exception to be thrown.

void

SQLException

Yes

rollback()

Undoes all changes made in the current transaction and releases any database locks currently held by this connection object. This method should be used only when the autocommit mode is disabled.

void

SQLException

Yes

rollback(Savepoint savepoint)

Undoes all changes made after a given savepoint object is set. This method should be used only when the autocommit mode is disabled.

void

SQLException

Yes

setAutoCommit(boolean autoCommit)

Sets the autocommit mode of this connection to the given state. If a connection is in autocommit mode, all its SQL statements are executed and committed as a single transaction. Otherwise, its SQL statements are grouped into transactions that are terminated by calls to method commits or method rollbacks. By default, new connections are in autocommit mode.

void

SQLException

Yes

setClientInfo(Properties properties)

Sets the client information property value of the connection. The properties object contains the names and values of the client information properties to be set. The client information property set contained in the attribute list replaces the current client information property set on the connection. If the property currently set on the connection is not in the property list, the property is cleared.

void

SQLClientInfoException

Yes

setClientInfo(String name,String value)

Sets the client information property value specified by name to that specified by value.

void

SQLClientInfoException

Yes

setHoldability(int holdability)

Changes the default holdability of the ResultSet object created with this connection object to the given holdability. The default holdability of the ResultSet object can be determined by calling DatabaseMetaData.getResultSetHoldability.

void

SQLException

Yes

setNetworkTimeout(Executor executor, int milliseconds)

Sets the maximum time that a connection or an object created from a connection waits for the database to reply to any request. If any request is still not answered, the waiting method returns SQLException, and the connection or object created from the connection is marked as closed. Any subsequent use of the object other than the Close, isCabled, or Connection.isValid method will result in SQLException.

void

SQLException

Yes

setReadOnly(boolean readOnly)

Sets this connection to read-only mode as a hint for the driver to enable database optimization.

void

SQLException

Yes

setSavepoint()

Creates an unnamed savepoint in the current transaction and returns a new savepoint object representing it.

Savepoint

SQLException

Yes

setSavepoint(String name)

Creates a savepoint with the given name in the current transaction and returns a new savepoint object representing it.

Savepoint

SQLException

Yes

setSchema(String schema)

Sets the given schema name to access.

void

SQLException

Yes

setTransactionIsolation(int level)

Attempts to change the transaction isolation level of this connection object to a given level. Constants defined in the connection interface are possible transaction isolation levels.

void

SQLException

Yes

setTypeMap(Map<String,Class<?>> map)

Installs the given TypeMap object as the type mapping for this connection object. Type mappings will be used for SQL structured types and custom mappings for different types.

void

SQLException

Yes

  1. The autocommit mode is used by default within the API. If you disable it by running setAutoCommit(false), all the statements executed later will be packaged in explicit transactions, and you cannot execute statements that cannot be executed within transactions.
  2. Fully-encrypted databases cannot use setClientInfo("send_token", null) to transmit keys or use setClientInfo("clear_token", null) to destroy keys.