JDBC Drivers in Java

Naveen AnamaneniUpdated · 7 min read

A JDBC driver is a software component that lets a Java application talk to a specific database. It implements the JDBC interfaces (Connection, Statement, ResultSet and the rest) and translates each call into something the database understands. Drivers are traditionally grouped into four types, according to how they reach the database. This classification is a favorite interview topic. In practice, almost every driver used today is Type 4. In this lesson, you will learn the four types, their advantages and disadvantages, and how Java finds and loads a driver.

The Four Types

From bridge to pure Java: only Type 4 needs nothing else installed
TypeNameHow it worksWritten in
1JDBC-ODBC bridgeConverts JDBC calls into ODBC calls; an ODBC driver talks to the databaseJava + native
2Native-API driverConverts JDBC calls into calls to the database's own client libraryJava + native
3Network protocol driverSends calls to a middleware server, which talks to the databasePure Java
4Thin driverSpeaks the database's network protocol directlyPure Java

Type 1: JDBC-ODBC Bridge

AdvantagesDisadvantages
Worked with any database that had an ODBC driverSlow: every call is translated twice
Easy to try in the early days of JavaODBC must be installed and configured on every client machine
Not portable; removed from the JDK in Java 8

Type 2: Native-API Driver

AdvantagesDisadvantages
Faster than Type 1The vendor's native library must be installed on every client
Can use vendor-specific featuresTied to an operating system; a native crash can take down the JVM

Oracle's OCI driver is an example.

Type 3: Network Protocol Driver

AdvantagesDisadvantages
Pure Java on the client; nothing native to installNeeds a separate middleware server to run and maintain
One driver can reach several databases through the middlewareAn extra network hop adds delay

Type 4: Thin Driver

AdvantagesDisadvantages
Pure Java: one JAR, works on every platformOne driver per database
Talks directly to the database: the fastest route
Nothing to install on the client

MySQL Connector/J, the PostgreSQL JDBC driver, Oracle's thin driver and the Microsoft SQL Server driver are all Type 4.

How a Driver Is Loaded

Since JDBC 4.0 (Java 6), you only put the driver's JAR on the class path. The JAR contains a small file, META-INF/services/java.sql.Driver, naming its driver class, and DriverManager loads it automatically. The old line Class.forName("com.mysql.cj.jdbc.Driver") is no longer needed.

Let's see an example that lists the registered drivers and shows what happens when no driver matches a URL:

Drivers.java▶ Try it
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class Drivers {
    public static void main(String[] args) throws Exception {
        DriverManager.drivers().forEach(d -> System.out.println(
            "Registered: " + d.getClass().getName()
            + " (JDBC compliant: " + d.jdbcCompliant() + ")"));
 
        Driver driver = DriverManager.getDriver("jdbc:h2:mem:school");
        System.out.println("Accepts jdbc:h2:...?    "
            + driver.acceptsURL("jdbc:h2:mem:school"));
        System.out.println("Accepts jdbc:mysql:...? "
            + driver.acceptsURL("jdbc:mysql://localhost:3306/school"));
 
        try (Connection con =
                 DriverManager.getConnection("jdbc:h2:mem:school")) {
            DatabaseMetaData md = con.getMetaData();
            System.out.println(md.getDriverName() + ", JDBC "
                + md.getJDBCMajorVersion() + "." + md.getJDBCMinorVersion());
        }
 
        try {
            DriverManager.getConnection("jdbc:mysql://localhost:3306/school");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
        }
    }
}
Output
Registered: org.h2.Driver (JDBC compliant: true)
Accepts jdbc:h2:...?    true
Accepts jdbc:mysql:...? false
H2 JDBC Driver, JDBC 4.3
SQLException: No suitable driver found for jdbc:mysql://localhost:3306/school

Explanation:

  • DriverManager.drivers() lists every driver found on the class path. Only the H2 JAR is there, so only its driver appears.
  • acceptsURL is how a driver is chosen: DriverManager asks each registered driver whether it understands the URL. H2 accepts jdbc:h2:... and rejects jdbc:mysql:....
  • "No suitable driver found" is the error when no registered driver accepts the URL. It almost always means the driver JAR is missing from the class path, or the URL has a typo.

Common Drivers

DatabaseDriver classURL starts with
MySQLcom.mysql.cj.jdbc.Driverjdbc:mysql://
PostgreSQLorg.postgresql.Driverjdbc:postgresql://
Oracleoracle.jdbc.OracleDriverjdbc:oracle:thin:@
SQL Servercom.microsoft.sqlserver.jdbc.SQLServerDriverjdbc:sqlserver://
H2org.h2.Driverjdbc:h2:
SQLiteorg.sqlite.JDBCjdbc:sqlite:

Common Mistakes

Driver JAR not on the class path. "No suitable driver found for ..." is the result.

A typo in the URL prefix. jdbc:mysql//... (missing colon) matches no driver.

Using an old driver class name. MySQL's is com.mysql.cj.jdbc.Driver; the older com.mysql.jdbc.Driver is deprecated.

Interview Corner

What are the types of JDBC drivers?

Type 1, the JDBC-ODBC bridge, translates JDBC calls to ODBC; it needs ODBC on the client and was removed in Java 8. Type 2, the native-API driver, calls the database's native client library, so that library must be installed on each client. Type 3, the network protocol driver, is pure Java and sends requests to a middleware server that talks to the database. Type 4, the thin driver, is pure Java and communicates directly with the database using its network protocol.

Which JDBC driver type is preferred and why?

Type 4. It is written entirely in Java, so it is platform-independent and needs nothing installed on the client beyond a JAR, and it talks directly to the database without translation layers, which makes it the fastest and simplest to deploy. Modern drivers for MySQL, PostgreSQL, Oracle and SQL Server are all Type 4.

Practice

  1. Easy: For each of the four types, write one sentence saying what must be installed on the client machine.
  2. Harder: Run Drivers without the H2 JAR on the class path and explain the output.

Quick check: JDBC drivers

4 questions
1Which driver type is pure Java and talks directly to the database?
2Which driver type was removed from the JDK in Java 8?
3What does "No suitable driver found" usually mean?
4Is Class.forName needed to load a JDBC 4 driver?

Key Takeaways

Was this tutorial helpful?

Lesson 484 of 578