Understanding Connection Interface

The Connection interface belongs to the java.sql package and represents a connection between a Java application and a database. It provides methods for creating Statement, PreparedStatement, and CallableStatement objects. It is also used to manage database transactions such as commit, rollback, and savepoints. A Connection object is normally obtained using DriverManager.getConnection(). After completing database operations, the connection should be closed to release database resources.

Methods of Connection Interface

Methods

public Statement createStatement() throws SQLException

Creates a Statement object that can be used to execute SQL queries.

public PreparedStatement prepareStatement(String sql) throws SQLException

Creates a PreparedStatement object for executing parameterized SQL queries.

public CallableStatement prepareCall(String sql) throws SQLException

Creates a CallableStatement object for executing stored procedures.

public void setAutoCommit(boolean autoCommit) throws SQLException

Enables or disables automatic transaction commit.

public boolean getAutoCommit() throws SQLException

Returns whether auto-commit mode is enabled.

public void commit() throws SQLException

Permanently saves all changes made during the current transaction.

public void rollback() throws SQLException

Rolls back all changes made since the previous commit or rollback.

public void close() throws SQLException

Closes the database connection and releases JDBC resources.

public boolean isClosed() throws SQLException

Checks whether the connection has been closed.

public DatabaseMetaData getMetaData() throws SQLException

Returns information about the database and JDBC driver.

public boolean isValid(int timeout) throws SQLException

Checks whether the database connection is valid within the specified timeout.

public void setReadOnly(boolean readOnly) throws SQLException

Sets the connection to read-only or read-write mode.

public boolean isReadOnly() throws SQLException

Checks whether the connection is in read-only mode.

public void setTransactionIsolation(int level) throws SQLException

Sets the transaction isolation level for the connection.

public int getTransactionIsolation() throws SQLException

Returns the current transaction isolation level.

Scroll to Top