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 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-Protocal Pure Java Driver (Type-3)
- Native-Protocal Pure Java Driver (Type-4)
JDBC-ODBC Bridge Driver (Type-1) :
This Type 1 driver is developed by SUN micro systems. 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 data base with the help of ODBC API. Hence it is called as JDBC-ODBC bridge driver.
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.Using Class.forName :
2.Using DriverManager.registerDriver :
The DriverManager class contains the following method which will load the driver at runtime.
public static void registerDriver (java.sql.Driver);
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 hand over into database environment through Data Source Name. odbc is the sub protocol which takes the database result and gives to 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 is username and password.
Example to get the Connection :
Connection con1=DriverManager.getConnection (“jdbc : odbc : Access”);
Connection con2=DriverManager.getConnection (“jdbc : odbc : oracle”,”scott”,”tiger”);
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.
Drawbacks of Type-1 Driver :
- The Type-1 driver is very slow, because of JDBC-ODBC transactions.
- The Type-1 driver is dependent driver, since its depending on ODBC support.
- The Type-1 driver is not suitable for real time applications.
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.
Data base vendor provides either native api or native protocol to connect with database. These is no guarantee that every database vendor supplies both api and as well as protocol; some vendors supply only native protocol and some supply both.
For example Oracle corp hs given both native api and native protocol. Where as mySql has given only native protocol.
Type 2 driver converts the JDBC calls into native calls, to connect with the database.
Type-2 Oracle Drivers :
Oracle corporation has given 2 JDBC Driver softwares :
- Oracle OCI Driver – Type-2
- Oracle Thin Driver – Type-4
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 class path.
Advantages of Type -2 Driver :
- Type – 2 driver is comparatively faster than Type -1 driver.
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.
Net-Protocal 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 application server. The JDBC clients uses the standard network sockets to communicate with the middle ware application server.
The middle ware application server internally converts the JDBC calls to vendor’s specific database protocol.
Advantages of Type -3 Driver :
- While using the Type – 3 Driver, it is not required to maintain libraries at client side, since the application server itself can performs the tasks.
Drawbacks of Type-3 Driver :
- Since it is a network driver, client system need to support the network.
- Data base specific coding is required for middle layer application. Hence it require more maintenance.
Native-Protocal 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 Type-4 driver, we need to add the classes111.jar into our class path.
The Type-4 driver uses native protocol accepted by database server to establish connection between a java program and database.
It is a pure Java driver, because it completely implemented by the Java programming language
Example to get the Connection :
Connection con=DriverManager.getConnection (“jdbc : oracle : thin :@ localhost : 1521 : chandrashekhar”, “scott”, “tiger”);
On the above connection syantax, “thin” is a type 4 is a driver name. The thin driver converts the JDBC calls to vendor’s specific database calls.
Advantages of Type -4 Driver :
- It is a fastest driver among all types of drivers.
- It is a platform independent driver.
- This Type-4 driver is suitable for developing real time applications.
Drawbacks of Type-4 Driver :
- The only drawback of Type-4 driver is, it is database dependent.