Java Connection interface

Last Updated : 8 Apr 2026

The Connection interface is an important part of JDBC that helps a Java application communicate with a database. It represents an active connection where database operations are performed.

In this chapter, you will learn about the Connection interface, its role in JDBC, and how it is used to interact with databases.

What is Connection Interface?

A Connection is a session between a Java application and a database. It is used to establish a connection and perform operations on the database.

The Connection interface acts as a factory for Statement, PreparedStatement, and DatabaseMetaData objects. It also provides methods for transaction management such as commit(), rollback(), setAutoCommit(), and setTransactionIsolation().

Importing / Using Connection Interface

To use the Connection interface in a program, we need to import it from the java.sql package.

Here is the syntax to important Connection interface:

Or, we can import all JDBC classes using:

Methods of Connection Interface

The following table lists some commonly used methods of the Connection interface along with their descriptions:

MethodDescription
public Statement createStatement()Creates a Statement object that is used to execute SQL queries.
public Statement createStatement(int resultSetType, int resultSetConcurrency)Creates a Statement object with specified result set type and concurrency.
public void setAutoCommit(boolean status)Sets the commit mode. By default, it is true (auto-commit enabled).
public void commit()Saves all changes made since the last commit or rollback permanently.
public void rollback()Cancels all changes made since the last commit or rollback.
public void close()Closes the connection and releases JDBC resources immediately.

Connection Interface Fields

There are some common Connection interface constant fields that are present in the Connect interface. These fields specify the isolation level of a transaction:

  • TRANSACTION_NONE: No transaction is supported, and it is indicated by this constant.
  • TRANSACTION_READ_COMMITTED: It is a constant which shows that the dirty reads are not allowed. However, phantom reads and non-repeatable reads can occur.
  • TRANSACTION_READ_UNCOMMITTED: It is a constant which shows that dirty reads, non-repeatable reads, and phantom reads can occur.
  • TRANSACTION_REPEATABLE_READ: It is a constant which shows that the non-repeatable reads and dirty reads are not allowed. However, phantom reads and can occur.
  • TRANSACTION_SERIALIZABLE: It is a constant which shows that the non-repeatable reads, dirty reads as well as the phantom reads are not allowed.

Example of Connection Interface

The following example demonstrates how to use the Connection interface to connect to a database and perform a simple query.

Output:

1 Rahul
2 Priya
3 Amit

Explanation:

This example shows how a program connects to a database using the Connection interface, executes a query, and displays the result. The connection is created using DriverManager, and data is fetched using a Statement and ResultSet.