JDBC Driver Types Explained

What is a JDBC driver?

To connect to a database from a Java program, a JDBC driver made specifically for the database you are connecting to must be used.

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

  • To connect to a MySQL database, you need to download a MySQL JDBC driver
  • To connect to an HyperSQL database, you need to download an HSQLDB JDBC driver
  • To connect to a DB2 database, you need to download a DB2 JDBC driver
  • To connect to an Oracle database, you need to 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 loaded 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.

Since the most common protocol covered by the type 1 JDBC driver is 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.

DB2 offers a type 2 JDBC driver that is written in C++, is extremely fast and efficient, and connects to a custom piece of 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. But there are times 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.

The type 4 JDBC driver also connects directly to the target database either through the use of Network APIs or Java I/O calls.

Because there is no native code involved, and there is no need for SQL queries to go through a separate piece of middleware, the type 4 JDBC driver is often referred to as the thin JDBC driver.

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

Is there a type 5 JDBC driver?

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

Vendors will often tout some incredible new technology or advancement in science that will set their database driver apart from all the rest, and they label this 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, that will make the question of which JDBC driver type to use a very easy one.

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.

And a type 4 JDBC driver that does not require any middleware to be installed is preferred over a type 3 JDBC driver, as installation, maintenance and migration is much easier to perform.

When should you choose a native API JDBC driver type?

And there are times when a type-2, native-API driver is the right choice for JDBC.

Sometimes a type-2 driver will provide connectivity to non-standard systems that are outside the capabilities of a thin JDBC driver. And the middleware piece a type-2 or type-3 driver connects to might provide special features for implementing server-side security or two-phase transactional commits that might not be available 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