Driver Registration In JDBC JDBC Connection Using the JDBC Driver Registering the JDBC driver Registering A JDBC driver JDBC Database Connections Basics Of JDBC How To Establish A JDBC Connection

Driver Registration In JDBC

An interface links two unrelated objects.A driver in JDBC context is an interface which links java code to database for communication.

Before ever the connection is established between the java programming end and the database end,the driver needs to be registered with the JVM by the means of DriverManager class.The DriverManager class has a vector of drivers registered to it.Once the entry has been made in the vector of the DriverManager class the driver is said to be registered.Each driver has the implementation and is capable of registering itself with the DriverManager class.This implementation is written in the static initialization block of the driver class.

We know that the static initialization block for a class is executed every time we load the class in the class loader. So, in order to run the static initialization block in the class loader we need to run the following line of code so that the driver registers itself with the DriverManager class :

Class.forName("DriverClass");

We can load or compile a class in class loader by calling the Class.forName(“class name”) method of the reflection concept in java.

Now let us have a look at how  the static initialization block in the driver class looks like :

 

class Driver{

 static{
    --------
    DriverManager.registerDriver(Driver Class);
    ----
    ----
    ----
    }
}

 

The function registerDriver() of the DriverManager class takes driver class as one of the parameters and looks for an entry ofr that driver in the vector it has. If an entry is found it means that the driver is already registered and the reference is returned from the vector.UIf entry is not available in the vector,the entry is made and reference is returned.This way we say that the driver is registered and connection can be made with the database as follows :

DriverManager.getConnection("Connection String","username","password");

Here connection String is depenedent on what database we are using.A typical example is :

jdbc:postgresql://host:port/database

Here ,username and password are username and password for the database.

Number of View :611

Tags: , , , , , , , ,

  1. No comments yet.

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache