Back to: Java Tutorials For Beginners and Professionals
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.
- What are JDBC Drivers in Java?
- What are the Similarities between the Four types of JDBC Drivers?
- Type 1 JDBC Driver (JDBC-ODBC Bridge driver)
- Type 2 JDBC Driver (Native API Partly Java Driver)
- Type 3 JDBC Driver (NET Protocol All Java Driver)
- Type 4 JDBC Driver (Native Protocol All Java Driver)
- 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
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 :
- JDBC-ODBC bridge driver (TYPE-1)
- Native API, partly java driver (TYPE-2)
- Net protocol, All java driver(TYPE-3)
- Native protocol, All java driver (TYPE-4)
What are the Similarities between the Four types of JDBC Drivers?
- All our translation software’s written in java according to JDBC specifications.
- All implement JDBC API.
- 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.
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:
- It is the only driver to communicate with legacy databases.
- 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:
- Makes the java application OS-dependent (Windows).
- 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.
- Client-side maintenance is very high.
- Another requirement is that the ODBC driver and the native database connectivity library must already be installed on the client machine.
- 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 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:
- Better performance than Type I driver as ODBC layer is completely eliminated and it uses native API which is database specific.
- OS in-dependency is achieved i.e. Type 2 drivers are available for all platforms of java.
- Client-side maintenance is slightly reduced.
Disadvantages of Type 2 Driver:
- It doesn’t provide optimum performance as it doesn’t directly communicate with the database.
- Because of native libraries, there is a chance of a system crash.
- Client-side maintenance is not completely eliminated. If DBMS product changes, in all client machines that vendor-provided native library has to be copied.
- 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 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:
- Zero client-side maintenance
- Better performance as connection optimization can be done in the network server and protocols are faster.
- This driver is server-based so there is no need for any vendor database library to be present on the client machine.
- This driver is fully written in java and hence portable. It is suitable for the internet.
Disadvantages of Type 3 Driver:
- It makes java database communication a 3 tier architecture that is complex to maintain.
- Type 3 driver requires database-specific coding to be done in the middle tier. Maintenance of the middle tier becomes costly.
- 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 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:
- Zero client-side maintenance.
- 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 :
- Use Type 4 driver for the applet to database communication
- 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.
- 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.
- 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.