jdbc Getting started with jdbc

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


JDBC, or Java DataBase Connectivity, is the Java specification for connecting to (relational) databases. JDBC provides a common API in the form of a number of interfaces and exceptions, and expectations (or requirements) of drivers.

The JDBC specification consists of two parts:

  1. A specification document, available from the JSR-221 page
  2. The API and its documentation, included with the Java SE API (packages java.sql and javax.sql)

Most relational databases, and some non-relational databases, provide a driver that implements the JDBC.


VersionRelease Date

Creating a connection

To be able to use JDBC you need to have the JDBC driver of your database on the class path of your application.

There are multiple ways to connect to a database, but the common ways are to either use the java.sql.DriverManager , or to configure and use a database specific implementation of javax.sql.DataSource .

A simple example to create a connection to a database with the url jdbc:somedb://localhost/foobar and execute an update statement to give all employees a 5% raise:

try (Connection connection = DriverManager.getConnection(
        "jdbc:somedb://localhost/foobar", "anna", "supersecretpassword");
     Statement updateStatement = connection.createStatement()) {
    updateStatement.executeUpdate("update employees set salary = salary * 1.05");

For further details see creating a database connection

Got any jdbc Question?