JDBC Drivers

JDBC Drivers in Java Application

In this article, I am going to discuss JDBC Drivers in Java Applications. Please read our previous article where we discussed JDBC Architecture in Java Application. At the end of this article, you will understand the following pointers in detail which are related to Java JDBC Drivers.

  1. What are JDBC Drivers in Java?
  2. What are the Similarities between the Four types of JDBC Drivers?
  3. Type 1 JDBC Driver (JDBC-ODBC Bridge driver)
  4. Type 2 JDBC Driver (Native API Partly Java Driver)
  5. Type 3 JDBC Driver (NET Protocol All Java Driver)
  6. Type 4 JDBC Driver (Native Protocol All Java Driver)
  7. How one can choose the right Driver?
What are JDBC Drivers?

The driver is an interface that existed between Java application and database to map Java API calls to Query language API calls and Query language API calls to Java API calls. To provide a driver as a product Sun Microsystems has provided a Driver as an interface and Sun Microsystems lets the database vendors provide implementation classes to the driver interface as part of their database software. If we want to use Drivers in JDBC applications then we have to get Driver implementation from the respective database software.

Driver Class is a class that implements the driver interface of the java.sql package. The driver class name is different from the database to the database. For example,

For Oracle : oracle.jdbc.driver.OracleDriver

For MySQL : com.mysql.jdbc.Driver

What are JDBC Drivers

Note: JDBC specification is a set of rules framed by SUN Microsystems for vendors to bring standardization in JDBC driver manufacturing.

JDBC compliant drivers are used in java enterprise applications for back-end communication. There is 180+ numbers of drivers but all these drivers could be classified into the following four types :

  1. JDBC-ODBC bridge driver (TYPE-1)
  2. Native API, partly java driver (TYPE-2)
  3. Net protocol, All java driver(TYPE-3)
  4. Native protocol, All java driver (TYPE-4)
What are the Similarities between the Four types of JDBC Drivers?
  1. All our translation software’s written in java according to JDBC specifications.
  2. All implement JDBC API.
  3. All enable java-database communication.
Type 1 JDBC Driver (JDBC-ODBC Bridge driver)

This is the first driver ever developed. The driver class of type1 driver software is “sun.jdbc.odbc.JdbcOdbcDriver”. This driver receives any JDBC method calls and sends them to ODBC (Open Database Connectivity) driver. ODBC driver understands these calls and communicates with the database library provided by the vendor. So the ODBC driver and the vendor database library must be present in the client machine.

As the type1 driver acts as a bridge between sun Microsystems JDBC technology and Microsoft’s ODBC technology it is known as the JDBC-ODBC bridge driver.

Type 1 JDBC Driver (JDBC-ODBC Bridge driver)

JDBC-ODBC Driver is a driver provided by Sun Microsystems as an implementation of the Driver interface. Sun Microsystems has provided JDBC-ODBC Driver with the interdependent on the Microsoft’s product ODBC Driver. ODBC Driver is an Open Specification, it will provide a very good environment to interact with any type of database from JDBC-ODBC Driver. If we want to use JDBC-ODBC Driver in our JDBC Applications first we have to install the Microsoft Product ODBC Driver native library.

To interact with the database from Java Application if we use JDBC-ODBC Driver then we should require two types of conversions so that JDBC-ODBC Driver is a slower Driver. JDBC-ODBC Driver is highly recommended for stand-alone applications, it is not suitable for web applications, distributed applications, and so on. JDBC-ODBC Driver is suggestable for Simple JDBC applications, not for complex JDBC applications. The portability of the JDBC-ODBC Driver is very less.

Advantages of Type 1 Driver:
  1. It is the only driver to communicate with legacy databases.
  2. The JDBC-ODBC Bridge driver allows access to almost all databases since the database’s ODBC drivers are already available in the client machine.
Disadvantages of Type 1 Driver:
  1. Makes the java application OS-dependent (Windows).
  2. The performance of this driver is less because the JDBC calls go through the bridge driver to the ODBC driver and then to the native database connectivity library. The result comes back through the reverse process. That means in simple words we can say bad performance due to two-layer translation.
  3. Client-side maintenance is very high.
  4. Another requirement is that the ODBC driver and the native database connectivity library must already be installed on the client machine.
  5. Not good for the web.
Type 2 JDBC Driver (Native API Partly Java Driver)

Type 2 JDBC driver is known as Native API Partly Java Driver. DBMS vendor only develops type 2 drivers.

The Type 2 driver translates JDBC method calls into DBMS vendor-provided native library calls. These native libraries are developed using C or C++ which means java software known as Type 2 driver makes use of JNI (Java Native Interfaces) to invoke the native library. Therefore it is called Partly Java Driver.

For example, oracle’s OCI driver

Class name = oracle.jdbc.driver.OracleDriver

Connection string = jdbc:oracle:OCI8:@server

Type 2 JDBC Driver (Native API Partly Java Driver)

Type 2 Driver is also called part java, the part native driver that is Type 2 Driver was implemented by using Java implementations and the database vendor provided the native library. When compared to Type 1 Driver, Type 2 Driver is a faster Driver because it should not require two-time conversions to interact with the database from Java Applications.

When compared to Type 1 Driver, Type 2 Driver portability is more. Type 2 Driver is still recommended for stand-alone applications not suggestible for web applications and Enterprise applications. If we want to use Type @ Driver in our JDBC applications then we have to install the database vendor-provided native library. Type 2 Driver is cast full Driver among all the drivers. Type 2 Diver’s portability is not good when compared to Type 3 Driver and Type 4 Driver.

Advantages of Type 2 Driver:
  1. Better performance than Type I driver as ODBC layer is completely eliminated and it uses native API which is database specific.
  2. OS in-dependency is achieved i.e. Type 2 drivers are available for all platforms of java.
  3. Client-side maintenance is slightly reduced.
Disadvantages of Type 2 Driver:
  1. It doesn’t provide optimum performance as it doesn’t directly communicate with the database.
  2. Because of native libraries, there is a chance of a system crash.
  3. Client-side maintenance is not completely eliminated. If DBMS product changes, in all client machines that vendor-provided native library has to be copied.
  4. The vendor database library needs to be loaded on each client machine and hence can not be used for the internet.
Type 3 JDBC Driver (NET Protocol All Java Driver)

Type 3 driver is known as Net Protocol All Java (Pure Java) Driver. The Type 3 driver translates JDBC method calls into a network server understandable protocol call. That network server (middleware server) internally uses a Type1 or Type 2 driver to communicate with the DBMS.

Type 3 JDBC Driver (NET Protocol All Java Driver)

Type 3 is also called Middleware Database server access Driver and Network Driver. Type 3 Driver is purely designed for Enterprise applications it is not suggestible for standalone applications. Type 3 Driver portability is very good when compared to Type 1 and Type 2 Drivers. Type 3 drivers will provide a very good environment to interact with multiple databases.

Type 3 Driver will provide a very good environment to switch from one database to another database without having modifications in client applications. Type 3 Driver should not require any native library installations, it should require compatibility with the application server. Type 3 Driver is the fastest Driver as compared to all the Drivers.

Advantages of Type 3 Driver:
  1. Zero client-side maintenance
  2. Better performance as connection optimization can be done in the network server and protocols are faster.
  3. This driver is server-based so there is no need for any vendor database library to be present on the client machine.
  4. This driver is fully written in java and hence portable. It is suitable for the internet.
Disadvantages of Type 3 Driver:
  1. It makes java database communication a 3 tier architecture that is complex to maintain.
  2. Type 3 driver requires database-specific coding to be done in the middle tier. Maintenance of the middle tier becomes costly.
  3. This driver also doesn’t directly communicate with the database. Therefore, still, there is a scope for improvement in performance.
Type 4 JDBC Driver (Native Protocol All Java Driver)

Type 4 JDBC driver is also known as Native Protocol All Java Driver. It translates JDBC method calls into DBMS-specific protocol calls so that client applications can communicate directly with the database server. For example, the oracle thin driver translates JDBC method calls into SQL *NET protocol calls. TYPE 4 drivers are completely implemented in Java to achieve platform independence and eliminate deployment administration issues. Hence it is most suitable for the web.

Type 4 JDBC Driver (Native Protocol All Java Driver)

Type 4 Driver is also called pure Java Driver and Thin Driver because Type $ driver was implemented completely by using java implementations. Type 4 Driver is the frequently used Driver when compared to all the remaining Drivers.

Type 4 Driver is recommended for any type of application including standalone applications, Network applications, etc. Type 4 Driver portability is very good when compared to all the remaining drivers. Type 4 Driver should not require any native library dependencies and it should require one-time conversion to interact with the database from Java applications. Type 4 is the cheapest Driver among all.

Advantages of Type 4 Driver:
  1. Zero client-side maintenance.
  2. Highest performance.
Disadvantages of Type 4 Driver:

With the type 4 driver, the user needs a different driver for each database. For example, to communicate with the oracle server we need an oracle driver and to communicate with the Sybase server we need a Sybase driver.

How one can choose the right Driver?

To improve performance :

  1. Use Type 4 driver for the applet to database communication
  2. Use Type 2 Driver for two tired applications for communication between the java client and the database that gives better performance when compared to Type 1 Driver.
  3. Use Type 1 Driver if your database doesn’t support a driver, This is a rare situation because almost all major databases support drivers or you will get them from third-party vendors.
  4. Use Type 3 Driver to communicate between client and proxy server (WebLogic, WebSphere, etc.) for three-tiered applications that give better performances when compared to Type 1 and Type 2 Drivers.

In the next article, I am going to discuss the Steps to Design JDBC Applications in Java with examples. Here, in this article, I try to explain JDBC Drivers in Java Applications. I hope you enjoy this JDBC Drivers in Java Application article.

Leave a Reply

Your email address will not be published. Required fields are marked *