A JDBC Driver is a software component, which allows a Java application to interact with a database. With a JDBC driver, an application is made capable of sending queries to a database and receiving results.
In this lesson, we will try to answer some simple but important questions related to these jdbc drivers like how they do what they do, how many types of drivers are there, etc. JDBC stands for Java DataBase Connectivity.
Table of Contents
How does JDBC driver works?
The Java JDBC API (Application Programming Interface) is a set of interfaces built upon the JavaSoft specification. These interfaces consist of a set of contract (or abstract) methods which its implementations must agree to, or must implement to conform to it. What this means is, the JDBC drivers are actually a set of classes that implement these interfaces.
As JDBC drivers follow the JavaSoft specification, they can virtually access any Database, execute commands with Spreadsheets and even some flat file formats! The only condition is that a JDBC driver must exist for that SQL based database.
JDBC Driver Types
In this section, we will explore the types of JDBC drivers. But wait! Didn’t we say that JDBC follows a specification, still there are multiple types? Yes, a specification just informs what should be done and not how it should be done.
Overall, JDBC driver types are used to categorize the technology used to connect to the database. Let’s see each of them here.
Type 1 – Bridge Drivers
Type 1 drivers are termed as Bridge Drivers. These bridge drivers translate all JDBC query calls into ODBC calls. This is an advantage as many drivers exist for the Open Database Connectivity (ODBC) technology which means, Type 1 drivers exist for all those databases as well.
Due to following disadvantages, Type 1 Driver is rarely used in production applications:
- Queries are slow as they need to go through ODBC conversion followed by native DB conversions.
- Bridge driver must be installed on host application which may not always be possible.
Type 2 – Native API
Type 2 Drivers are termed as Native Drivers. These native drivers translate all JDBC calls into native API database calls. Type 2 drivers are faster than Type 1 Bridge drivers as conversion happens directly to DB operations. This API isn’t completely written in Java as it access Native Database code as well.
Due to following disadvantages, Type 2 Drivers are rarely used in production applications:
- Client-side library for a Database has to be installed which may not always be possible.
- Not many database vendors provide native Client-side library.
Type 3 – Network Protocol
Type 3 Drivers are termed as Network Protocol or Middleware Drivers. These Network Protocol drivers translate all JDBC calls into database-specific calls. If a Java application needs to access multiple types of databases at the same time, type 3 is the preferred driver.
Type 3 Drivers are most used drivers in production applications due to following advantages:
- Type 3 JDBC drivers are the most flexible JDBC driver as they don’t need any native binary code on the client machine.
- The middleware application can do many other things apart from JDBC logic like Caching, Load balancing on Database servers etc. which is an overall performance boost for any JEE application.
Type 3 Driver does have some disadvantages as well:
- The middleware tier of the application needs some Database specific coding as well. This is usually an accepted solution due to the flexibility it offers.
- The middleware application may add a latency. But that can be improved by better coding standards etc.
Also, the driver manager class is a fully implemented class which connects an application to a data source, which is specified by a database URL.
Type 4 – Native Protocol
Type 4 Drivers are termed as Native Protocol or Middleware Drivers. These Native Protocol drivers translate all JDBC calls into direct vendor-specific database calls. The type 4 driver is written purely in Java and is platform independent.
Type 4 Drivers has a great advantage of it doesn’t needing any middleware application. Thus the performance is considerably improved. But Drivers are database specific and so, all vendors might not provide Type 4 driver for its usage.
How to determine which driver to use for JDBC?
We need to decide this based on the Database that we use in our application.
- In case of MySQL, we need to use jdbc driver for MySQL provided here.
- If case of Oracle, we can use jdbc driver provided by Oracle.here.
You can find JDBC drivers on the chosen DB’s official website. It is dependent on the JDK version as well as DB version.
In this lesson, we categorised 4 kinds of JDBC drives and looked at their advantages and disadvantages along with reasons to use them in production-grade web applications.
Feel free to share your views in comments below.