In this tutorial, we going to learn about JDBC Driver types and usages with examples.
What is JDBC Driver?
JDBC Driver is a software component which provides an implementation for interfaces of JDBC API. These JDBC API implementations are helpful to connect with the database from the Java applications. Hence the JDBC Driver acts as a mediator between a Java application and a database.
A JDBC driver is a product created by a vendor.
What is JDBC Specification?
A JDBC Specification is a document, it contains information needed for programmers and vendors to implement the JDBC drivers.
Programmer reads the specification to develop the applications and a vendor reads the specification to implement the drivers.
JDBC Driver Types :
There are four types of JDBC drivers available in the market:
- JDBC-ODBC Bridge Driver (Type-1)
- Native-API Driver (Type-2)
- Net-Protocol Pure Java Driver (Type-3)
- Native-Protocol Pure Java Driver (Type-4)
1. JDBC-ODBC Bridge Driver (Type-1):
This Type 1 driver is developed by SUN Microsystems. The name of the Type-1 driver is JDBC-Odbc Bridge Driver. The JDBC-Odbc Bridge Driver is found in a package called sun.jdbc.odbc. By using this driver we can develop only 2-tier applications (a Java program and database). This type of driver is purely implemented in ‘C’ language and this is platform dependent in nature.
This type-1 driver is used to connect the database with the help of the ODBC API. Hence it is called a JDBC-ODBC bridge driver.
1.1 Loading the drivers:
Loading the drivers is nothing but creating an object of appropriate Driver class. In order to load the drivers we have two ways, they are:
These two ways are common for all 4 types of JDBC drivers;
1. 2 Using Class.forName :
Class.forName(Sun.jdbc.odbc.JdbcOdbcDriver);
Class.forName(oracle.jdbc.driver.OracleDriver);
1.3 Using DriverManager.registerDriver :
The DriverManager class contains the following method which will load the driver at runtime.
public static void registerDriver (java.sql.Driver);
1.4 Obtaining the Connection with Type-1 Driver :
After loading the drivers, it is required to obtain the connection to connect with the database.
Main Protocol : Sub Protocol : Data Source
JDBC ODBC Oracle
Here, JDBC is the main protocol which takes java request and hands over into database environment through Data Source Name. odbc is the sub-protocol which takes the database result and gives to the Java environment. Data Source Name is the configuration tool in the current working machine through which the data is passing from java environment to database and database to Java environment.
In order to obtain the connection from the database, as a part of JDBC, we have a predefined
class called java.sql.DriverManager which contains the following methods:
- public static Connection getConnection (String URL);
- public static Connection getConnection (String URL, String username, String password);
Method-1, we use to obtain the connection from those databases where there is no username and password databases. Method-2 is used for those databases where there are username and password.
1.5 Example to get the Connection :
Connection con1=DriverManager.getConnection (“jdbc : odbc : Access”);
Connection con2=DriverManager.getConnection (“jdbc : odbc : oracle”,”scott”,”tiger”);
1.6 Advantages of Type -1 Driver :
- No need to install Type-1 driver separately, since it comes as part of the JDK.
- Type-1 driver is a database independent driver.
1.7 Drawbacks of Type-1 Driver :
- The Type-1 driver is very slow, because of JDBC-ODBC transactions.
- The Type-1 driver is the dependent driver since its depending on ODBC support.
- The Type-1 driver is not suitable for real-time applications.
2. Native-API Driver (Type-2) :
Type -2 driver uses native API supplied by a database vendor to connect with a database. Here native API means a set of functions written in C and C++ language.
Database vendor provides either native API or native protocol to connect with database. There is no guarantee that every database vendor supplies both API and as well as a protocol; some vendors supply only native protocol and some supply both.
For example, Oracle Corp has given both native API and native protocol. Whereas MySQL has given only native protocol.
Type 2 driver converts the JDBC calls into native calls, to connect with the database.
2.1 Type-2 Oracle Drivers :
Oracle Corporation has given 2 JDBC Driver software :
- Oracle OCI Driver – Type-2
- Oracle Thin Driver – Type-4
2.2 Oracle OCI Driver – Type-2 :
Oracle oci driver uses oci libraries to connect with the database.
The oci library contains “C” Language functions.
Oracle driver software doesn’t come along with JDK; it comes along with Oracle software.
Oracle driver comes in a jar file called odbc14.jar.
When we load Oracle driver into the JVM, it throws “ClassNotFoundException” because JVM only knows the classes and interfaces of Java API, but not given by the vendor.
In order to resolve the ClassNotFoundException, we need to add the ojdbc.jar in our classpath.
2.3 Advantages of Type -2 Driver:
- Type – 2 driver is comparatively faster than the Type -1 driver.
2.4 Drawbacks of Type-2 Driver:
- The Type-2 driver is both platform and database dependent. Hence it is not suitable for real-time applications.
- This driver is slower than the Type-3 and Type-4 drivers.
3. Net-Protocol Pure Java Driver (Type-3):
Net Protocol is a Type-3 JDBC driver. It was written in Java programming. This network protocol driver uses the middle software like an application server. The JDBC clients use the standard network sockets to communicate with the middleware application server.
The middleware application server internally converts the JDBC calls to the vendor’s specific database protocol.
3.1 Advantages of Type -3 Driver:
- While using the Type – 3 Driver, it is not required to maintain libraries at the client side, since the application server itself can perform the tasks.
3.2 Drawbacks of Type-3 Driver :
- Since it is a network driver, the client system needs to support the network.
- Database specific coding is required for middle layer application. Hence it requires more maintenance.
4. Native-Protocol Pure Java Driver (Type-4) :
Type-4 drivers are supplied by Oracle Corporation by developing into Java language. OracleDriver is the name of Type-4 driver which is released by Oracle Corporation in the form of classes111.jar. In order to work with the Type-4 driver, we need to add the classes111.jar to our classpath.
The Type-4 driver uses native protocol accepted by the database server to establish a connection between a java program and database.
It is a pure Java driver because it completely implemented by the Java programming language
4.1 Example to get the Connection :
Connection con=DriverManager.getConnection (“jdbc : oracle : thin :@ localhost : 1521 : chandrashekhar”, “scott”, “tiger”);
On the above connection syntax, “thin” is a driver name. The thin driver converts the JDBC calls to the vendor’s specific database calls.
4.2 Advantages of Type -4 Driver :
- It is the fastest driver among all types of drivers.
- It is a platform independent driver.
- This Type-4 driver is suitable for developing real-time applications.
4.3 Drawbacks of Type-4 Driver :
- The only drawback of Type-4 driver is, it is database dependent.
Happy Learning 🙂