JDBC Driver is a interface that enables JAVA application to interact with the database. The JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.
There are 4 types of JDBC drivers:
- Type 1: JDBC-ODBC bridge driver
- Type 2: Native-API driver (partially java driver)
- Type 3: Network protocol driver (fully java driver)
- Type 4: Thin driver (fully java driver)
Type 1 : JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function call (local native code). This is now discouraged because of thin driver.
JDBC-ODBC Bridge driver is not multi threaded which uses synchronized methods to serialize all the calls made to ODBC.
JDBC-ODBC Bridge can open only one Statement object per connection.
When to use?
The type 1 driver is not considered a deployment-level driver and is typically used for development and testing purposes only.
- easy to use.
- Easily connected to any database
- Performance degraded because JDBC method call is converted into the ODBC function calls.
- The ODBC driver needs to be installed on the client machine.
Type 2: Native-API driver (partially java driver)
The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.
When to use?
Type 2 drivers are useful in situations where a type 3 or type 4 driver is not available yet for your database.
- performance upgraded than JDBC-ODBC bridge driver.
- The Native driver needs to be installed on the each client machine.
- The Vendor client library needs to be installed on client machine.
Type 3: Network protocol driver (fully java driver)
The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.
When to use?
If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver.
- No client side library is required because of application server that can perform many tasks like load balancing, auditing , logging etc.
- Network support is required on client machine.
- Requires database-specific coding to be done in the middle tier.
- Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier.
Type 4: Thin driver (fully java driver)
The thin driver converts JDBC calls directly into the vendor-specific native database protocol. That is why it is known as thin driver. It is fully written in Java language.
When to use?
If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4.
- Better performance than all other drivers.
- No software is required at client sided or server side.
- Drivers depend on the database.
Note : Type 4 thin driver is fastest JDBC driver written in JAVA.
Some important questions and answers for generally asked in interview when interviewer want to check depth knowledge of interviewee.
What do you mean by fastest driver? Which type of JDBC driver is the fastest one?
JDBC driver performance or fastness depends on a number of issues Quality of the driver code, size of the driver code, database server and its load, Network topology, Number of times your request is translated to a different API.
Type 4: Thin driver (fully Java driver) is the fastest driver because it converts the JDBC calls into vendor specific protocol calls and it directly interacts with the database.
Is it possible to connect to multiple databases? Using single statement can we update or extract data from two or three or many databases?
Yes, it is possible to connect to multiple databases, at the same time, but it depends on the specific driver.
To update and extract data from the different database we can use the single statement. But we need middleware to deal with multiple databases or a single database.
How does JDBC handle the data types of Java and database?
The JDBC driver converts the Java data type to the appropriate JDBC type before sending it to the database. It uses a default mapping for most data types. For example, a Java int is converted to an SQL INTEGER.
More on JDBC
Follow below links to know more on JDBC and solving JDBC issues :