Jdbc-driver types with examples in Java

Jdbc-driver types with examples in Java

Before Discussing in detail about the type of Jdbc-driver  . We need to first understand the meaning of term Driver .
 So , What is Driver ?

The concept is that a Driver should be a one point contact for all interactions between your Java App. and the DB.

This is the core concept of JDBC



driver working









Read Also :       JDBC Interview Questions 


JDBC (Java Database Connectivity) a specification pitched in by the JCP team (from Java), which gives a contract level agreement between the JAVA and the Database

The specifications declared as part of JDBC are available as part of java.sql package.

Types of JDBC Drivers

The various types of JDBC Drivers are based on the WAY the above contract level agreement (shown in the image) is IMPLEMENTED by various coders.

That means , the JDBC specification given by the JCP team only defines the various interfaces, termed as JDBC API . It is up to the coders/developers/implementers who can implement those interfaces, in their own ways.

Based on the ways followed, we can classify them into four types.


JDBC Driver – Type 1   (JDBC ODBC Bridge)


This is an approach wherein the implemented class in Java makes calls to the code written in Microsoft languages (native), which speaks directly to the database.

The first category of JDBC drivers provides a bridge between the JDBC and the ODBC API . The bridge translates  the standard JDBC calls and sends them to the ODBC data source via ODBC libraries .


jdbc driver type 1 working








JDBC Driver – Type 2 ( Part Native Driver )

This is an approach wherein the implemented class in Java makes calls to the code written from the database provider (native), which speaks directly to the database.


jdbc driver type 2 working








JDBC Driver – Type 3

This is an approach wherein the implemented class in Java makes calls to the code written from application server providers, which speaks directly to the database.
More exploration on the way the Java Driver interacts with the Middleware is required here.

The Java client application sends a JDBC calls through a JDBC driver to the intermediate data access server ,which completes the request to the data source using another driver . This driver uses a database independent protocol , to communicate database request to a server component which then translate the request into a DB specific protocol .

jdbc driver type 3 working









JDBC Driver – Type 4  (Thin Driver)

This is an approach wherein the implemented class in Java (implemented by the database provider) speaks directly to the database.
In other words , it is a pure Java library that translates JDBC request directly to a  Database specific protocol .


jdbc driver type 4 working










JDBC Example



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample {
    
    public static void main(String args[]) {
        
        try {
            
            Class.forName("oracle.jdbc.driver.OracleDriver");
                                    ^
            <!--This is a Type 4 Driver. This implements the
            interface provided by the JDBC specification
            (java.sql.Driver)-->
            
            String url = "jdbc:oracle:thin:@10.184.132.128:1521:devdb";
            Connection conn = DriverManager.getConnection(url,"dev1201st","develop1201");
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("select sysdate from dual");
            
            while (rset.next()) {
                System.out.println(rset.getString(1));
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}