JDBC Drivers in Java
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
| Type | Name | How it works | Written in |
|---|---|---|---|
| 1 | JDBC-ODBC bridge | Converts JDBC calls into ODBC calls; an ODBC driver talks to the database | Java + native |
| 2 | Native-API driver | Converts JDBC calls into calls to the database's own client library | Java + native |
| 3 | Network protocol driver | Sends calls to a middleware server, which talks to the database | Pure Java |
| 4 | Thin driver | Speaks the database's network protocol directly | Pure Java |
Type 1: JDBC-ODBC Bridge
| Advantages | Disadvantages |
|---|---|
| Worked with any database that had an ODBC driver | Slow: every call is translated twice |
| Easy to try in the early days of Java | ODBC must be installed and configured on every client machine |
| Not portable; removed from the JDK in Java 8 |
Type 2: Native-API Driver
| Advantages | Disadvantages |
|---|---|
| Faster than Type 1 | The vendor's native library must be installed on every client |
| Can use vendor-specific features | Tied to an operating system; a native crash can take down the JVM |
Oracle's OCI driver is an example.
Type 3: Network Protocol Driver
| Advantages | Disadvantages |
|---|---|
| Pure Java on the client; nothing native to install | Needs a separate middleware server to run and maintain |
| One driver can reach several databases through the middleware | An extra network hop adds delay |
Type 4: Thin Driver
| Advantages | Disadvantages |
|---|---|
| Pure Java: one JAR, works on every platform | One 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:
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());
}
}
}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/schoolExplanation:
DriverManager.drivers()lists every driver found on the class path. Only the H2 JAR is there, so only its driver appears.acceptsURLis how a driver is chosen:DriverManagerasks each registered driver whether it understands the URL. H2 acceptsjdbc:h2:...and rejectsjdbc: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
| Database | Driver class | URL starts with |
|---|---|---|
| MySQL | com.mysql.cj.jdbc.Driver | jdbc:mysql:// |
| PostgreSQL | org.postgresql.Driver | jdbc:postgresql:// |
| Oracle | oracle.jdbc.OracleDriver | jdbc:oracle:thin:@ |
| SQL Server | com.microsoft.sqlserver.jdbc.SQLServerDriver | jdbc:sqlserver:// |
| H2 | org.h2.Driver | jdbc:h2: |
| SQLite | org.sqlite.JDBC | jdbc: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
- Easy: For each of the four types, write one sentence saying what must be installed on the client machine.
- Harder: Run
Driverswithout the H2 JAR on the class path and explain the output.
Key Takeaways
Was this tutorial helpful?