Friday, January 14, 2022

Why is JDBC Driver is an implementation of JDBC API ?

Why is JDBC Driver is an implementation of JDBC API ?

Since all the Driver Classes must mandatorily implement java.sql.Interfaces which is a part of JDBC API, Hence JDBC Driver is an implementation of JDBC API.

Definition of JDBC:- Java Database Connectivity [JDBC] is a specification given in the form of abstraction API to achieve loose coupling between Java Application and DB Server.

Steps of JDBC

There are 6 different steps present in JDBC:-

  1. Load and register the Driver[Driver refers as Driver Class].
  2. Establish the connection with a DB server.
  3. Create a platform or Statement.
  4. Execute the SQL query or SQL statements.
  5. Process the Resultant Data[Optional].
  6. Close all the connections ( Costly Resources ).

Specification of JDBC

There are 3 different specifications present for JDBC namely:-

  1. All the Driver Class must contain one Static Block in it.
  2. All the Driver Class must mandatory implements java.sql.interfaces which is a part of JDBC API.
  3. All the Driver Class must mandatorily be registered with DriverManager by using a static method called registerDriver() Method.

Ex:-

 Mysql (DataBase) :-

  import java.sql.*;

   + class Driver implements java.sql.interfaces

     {

        static

        {

  Driver d = new Driver();

           DriverManager.registerDriver( d ) ;

        }   

     }

  

1) Load and register the Driver[ Driver refers Driver classes ]:-

  • In these steps, we have to load and register the Driver classes which are a part of JDBC Driver and which are provided by the database vendors. The Driver classes loaded and registered in 2 different ways namely:-

  1.   Manually        
  2.  by using forName()


1)Manually:-

creating an object of Driver class and registered it with DriverManager by using static method called registeDriver() Method.

  

My-sql(DB Server):-

   Driver d=new Driver();

   DriverManager.registerDriver(d);


Oracle(DB Server):-

   OracleDriver od=new OracleDriver();

   DriverManager.registerDriver( od );


2)2nd way:-

By using static method called forName() Method, we can load and register the Driver classes which is a part of JDBC Driver.


 Syntax:-

lang package:- java.lang.*;

Mysql:-  Class.forName("com.mysql.jdbc.Driver"); 

             [ checked Exception(ClassNotFoundException) ]

Oracle:- Class.forName("oracle.jdbc.driver.OracleDriver"); 


  • It will load and register the Respective Driver Classes..  

2) Establish the connection with a DB Server:- 

  • In this step, We have to establish the connection between the java application and the DB Server by using " getConnection() " Method. 
  • Factory/ Helper Method:- It is used to create an implementation object.
  • getConnection():- It is  Factory/ Helper Method which is used to create and return the implementation object of " Connection " interface based on URL, Hence the                          Return type will be Connection interface, And it is static in nature.

There are 3 different overloaded variants(forms) of getConnection() Method which are as follows:-

  1.    getConnection(" URL")
  2.   getConnection(" URL " , Properties info)
  3.    getConnection(" URL " , " User ", " Password ") ;

  • getConnection()- present in a HelperClass called " DriverManager ".
  • Whenever we use getConnection() Method, it throws a checked Exception called SQL Exception.

java.sql.Connection:- It is an interface which is a part of JDBC API and the implementation are provided by the respective DB vendors as a part of JDBC Driver.

                    

java.sql.DriverManager:- It is a Helper class which is a part of JDBC API and which contains 2 important static methods in it namely:-

a)registerDriver()        

b)getConnection()


3) Create a Platform or Statement:-

  • we need to create a Platform or Statement in order to execute the SQL Queries or SQL Statements.
  • A Statement or Platform can be created either by using " Statement interface " or " PreparedStatement interface " or " CallableStatement interface ".


java.sql.Statement:- It is an interface that is a part of JDBC API and the implementations are provided by respective DB Vendors or Servers as a part of JDBC Driver.


Statement:- 

  • if I want to create the platform by using " Statement " interface, have to use " createStatement() " Method. 
  • " createStatement() " present in a Connection interface, It is a factory or helper method which is used to create and return the implementation object of " Statement " interface, Hence the return type for createStatement() is Statement interface.

4) Execute the SQL Query or SQL Statements:-

  • In order to execute the SQL queries or SQL statements,  we have 3 different methods present namely:-

  a) execute() 

  b) executeUpdate()

c) executeQuery()    [ DQL{ SELECT } , DML{ INSERT, UPDATE , DELETE} etc]

  • All these methods are present in Statement interface.

a) execute():-

  • execute Method is a Generic Method, Since it is used for any type of SQL Queries.
  • Hence, the return type is boolean.
  • By default, execute() returns a boolean True in case of DQL and boolean False in case of DML.

b) executeUpdate():-

  • executeUpdate() is a Specialized Method since it is used to execute Only DML Queries or DML Statement.
  • The outcome of DML is 0-n integer value which gives the total no. of records affected in the DB server.
  • Hence the return type is integer.
  • whenever we try to execute DQL using this method, it throws SQL Exception.

5) Process the Resultant Data[Optional].

6) Close the connection:-

  • We close the connection in a finally block, by using the if condition to avoid null pointer Exception.
  • All the interfaces in JDBC API, we have to close it...!!!!
  • Each Query is making one DB call.
  • The more no of DB calls, will decrease the performance of an application.
  • Each DB call is considered a costly resources.


0 comments:

If you have any doubts,please let me know