JDBC driver types explained

What is a JDBC driver?

To connect to a database from a Java program, you must use a JDBC driver made specifically for the database to which you want to connect.

Every vendor provides a custom-built JDBC driver to marshal SQL queries back and forth from your Java app to their specific database.

  • To connect to a MySQL database, you must download a MySQL JDBC driver.
  • To connect to an HyperSQL database, you must download an HSQLDB JDBC driver.
  • To connect to a DB2 database, you must download a DB2 JDBC driver.
  • To connect to an Oracle database, you must download an Oracle JDBC driver.

You get the point.

What are the 4 JDBC driver types?

If you dig into the API specification, you’ll notice there are four different JDBC driver types:

  1. The Type 1 JDBC driver is simply a JDBC-ODBC bridge.
  2. The Type 2 JDBC driver is written in a language other than Java, often C++ or C.
  3. The Type 3 JDBC driver talks to a middleware server first, not the database directly.
  4. The Type 4 JDBC driver is a pure, direct Java-to-the-database implementation.
JDBC Driver Type Example

Here we see the Derby JDBC driver loads first before a connection is made to the database from the Java program.

What is a Type 1 JDBC driver?

Type 1 JDBC drivers are not recommended for production systems. This JDBC driver type is simply a lightweight façade over another database protocol.

The most common protocol covered by the Type 1 JDBC driver is Open Database Connectivity (ODBC). This is often referred to as the JDBC-ODBC bridge.

What is a Type 2 JDBC driver?

Type 2 JDBC drivers are often used in production-based systems.

IBM DB2 offers a Type 2 JDBC driver, written in C++, that is extremely fast and efficient. It connects to custom IBM software that marshals SQL calls back and forth to databases hosted on z/OS, AS/400 machines and other Unix and iSeries systems.

As Java developers we always lean towards a 100% Java-based solution. There are times, however, when the use of another programming language makes more sense.

Because the Type 2 JDBC driver uses a language other than Java, this is often referred to as a native-API driver.

What is a Type 3 JDBC driver?

The Type 3 JDBC driver is written purely in Java, but it does not send SQL calls directly to the database.

Instead, a Type 3 JDBC driver communicates with a separate piece of software that then marshals requests back and forth to the targeted database.

The DataDirect SequeLink JDBC driver falls into the Type 3 category.

What is a Type 4 JDBC driver?

A Type 4 JDBC driver is written completely in Java. It also connects directly to the target database either through the use of Network APIs or Java I/O calls.

This JDBC driver is often referred to as the thin JDBC driver, because it involves no native code and no separate middleware to handle SQL queries.

The Type 4 JDBC driver is the most common of all the JDBC drivers. Most of the popular databases, including DB2, Derby, HSQLDB and MySQL, make them available for Java applications.

Is there a Type 5 JDBC driver?

You may hear talk of Type 5 JDBC drivers, but as far as the JDBC specification is concerned, there is no such thing.

Vendors tend to tout some new technology or scientific advancement that sets their product apart from all the rest. Database drivers are no different in this regard. Vendors might label their incredible, revolutionary feat as a Type 5 JDBC driver — but it’s all just marketing hype.

There are only 4 JDBC driver types.

Which JDBC driver type should I choose?

To choose the right JDBC driver type for your Java project, ask yourself the following questions:

  • Which JDBC drivers are available for my database?
  • Is there a pure, Java-based JDBC driver available?
  • Are there any custom features I might need from a native JDBC driver?

When do I use the JDBC ODBC bridge?

If there is only one JDBC driver type available for your database of choice, the question of which JDBC driver type to use becomes very easy.

For example, with Microsoft Access, the only JDBC driver type available is the JDBC-ODBC bridge. You don’t have any other options.

If there are options, a driver written purely in Java is preferred over a native JDBC driver.

Moreover, a Type 4 JDBC driver that requires no middleware is preferred over a Type 3 JDBC driver, because installation, maintenance and migration is much easier.

When should you choose a native API JDBC driver type?

There are times when a Type 2, native-API driver is the right choice for JDBC.

Sometimes a Type 2 driver provides connectivity to non-standard systems that are outside the capabilities of a thin JDBC driver. Also, a Type 2 or Type 3 driver might connect to the middleware piece that provides special features to implement server-side security or two-phase transactional commits that might be unavailable in a pure Java JDBC driver type.

If your application requires special features available only through a native JDBC driver type, then obviously the Type 2 driver is the right choice.

App Architecture
Software Quality
Cloud Computing