Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A JDBC Connection is a Java object representing a session with a specific database. An application uses that session to run SQL, receive results, and control transaction settings. To obtain one, include the database’s JDBC driver, provide a driver-specific JDBC URL and credentials, then use and close the connection.

What a JDBC Connection represents

A Connection is the application’s JDBC interface to an established database session. It is not the database itself, a SQL query, or the driver. The driver handles database-specific communication; the connection is the session through which Java code creates statements and interacts with the database.

The connection can also expose metadata about the database, including information about tables, supported SQL grammar, stored procedures, and connection capabilities. Its settings include transaction behavior, such as auto-commit and isolation level.

How a Java application gets a connection

For a small standalone program, the usual visible starting point is DriverManager.getConnection(...). It accepts a JDBC URL and can also receive credentials or a Properties object. The URL begins with the JDBC scheme, but the rest of its syntax and options are defined by the particular database driver. DriverManager selects an appropriate registered driver.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For current JDBC 4.0-or-later drivers found on the class path, driver discovery is ordinarily automatic. Older drivers may require manual loading; follow the installation instructions for the exact driver and version rather than adding Class.forName(...) automatically.

Connection failures can throw SQLException. The API also defines SQLTimeoutException for attempts that exceed the configured login timeout.

JDBC connection example with MySQL

This example assumes MySQL Connector/J is already available on the runtime class path. The URL follows the documented MySQL pattern; change the host, port, database name, and driver configuration to match your environment. Credentials are read from environment variables rather than embedded in source code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcConnectionExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/example_db";
        String user = System.getenv("DB_USER");
        String password = System.getenv("DB_PASSWORD");

        try (Connection connection =
                 DriverManager.getConnection(url, user, password)) {
            System.out.println("Connected: " + !connection.isClosed());
            // Create a PreparedStatement and work with the database here.
        } catch (SQLException e) {
            System.err.println("Database connection failed: " + e.getMessage());
        }
    }
}

Try-with-resources closes the Connection when execution leaves the block, including when an exception occurs. Once connected, the application can create a Statement or PreparedStatement and execute SQL through it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

DriverManager versus DataSource

Approach What the application does Typical context
DriverManager Calls getConnection with a JDBC URL and credentials; the setup is explicit in the code. A compact standalone example or simple connection flow.
DataSource Requests a connection from a configured source, making the underlying connection details less visible to application code. Application code where connection configuration should be abstracted. Java SE documentation identifies DataSource as the preferred means of connecting.

A DataSource does not by itself guarantee connection pooling; pooling depends on the implementation and its configuration.

Transactions and connection settings

Auto-commit is enabled by default: each statement is committed automatically. To group changes into a transaction, disable auto-commit, execute the required statements, and call commit() to save the changes. Use rollback() when the application needs to abandon the transaction after an error.

Connection settings can be managed with methods such as setAutoCommit and setTransactionIsolation. The appropriate isolation level depends on the database and the consistency requirements of the application.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

JDBC URL examples are database-specific

These examples illustrate different vendor-specific forms, not interchangeable URL templates:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • MySQL: jdbc:mysql://localhost:3306/
  • Java DB: jdbc:derby:testdb;create=true

A URL may specify a host or other location, a database name, and configuration properties. Check the documentation for the selected driver for its exact syntax and options; a URL written for one vendor is not generally portable to another.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.