Ways to register a JDBC Driver

We can register a JDBC driver in more than 10 ways. But there are majorly four ways to register a JDBC Driver. They are as follows:
  1. DriverManager.registerDriver();
  2. Class.forName();
  3. predefined System property(jdbc.driver);
  4. JDBC 4.0 Auto Loading of the JDBC Driver.

Using DriverManager.registerDriver(): 

          As we know(from FirstStep towards JDBC) that DriverManager is a class in java.sql package which contains the method registerDriver(Driver). Using this method we can register the driver.

Using Class.forName():

          First let us know what is this Class.forName() and where it is present. Let us open JSE API. There we can find the following syntax in java.lang.Class:
Fig: syntax of Class.forName();
          This method takes the absolute class path as its parameter. As this method is static it is called by using its class name Class. The work of Class.forName() method is to load the Driver class (the class which provides the implementation of Driver Interface) into JVM's memory. This Driver class consists a static block, which creates the object of itself and register the driver and register the driver as soon as the class is loaded. As we know that static blocks are executed as soon as the class is loaded. Hence the code present in this static block is responsible for registering the driver. The following example shows how to register the driver using Class.forName();
program for registering the driver using Class.forName()
Fig: program for registering the driver using Class.forName();

Using Predefined System Property-"jdbc.drivers":

           The third way to register the driver is by using the predefined system property named "jdbc.drivers". Before talking about this system property first let us know the internal code of DriverManager class. The following figure shows the internal code of DriverManager class.

DriverManager Class
Fig: Sample Interior code of DriverManager class
         Observe the above code carefully, DriverManager class consists a static block, which takes the system property. If we provide the system property in the command line, that system property is stored in a string and that string is passed as a parameter to the Class.forName(). We already know how Class.forName() registers the driver. Hence, we can register the driver by using "jdbc.drivers" system property. We cannot change the name of system property as it is predefined. The following program explains this:

using system property
Fig: using system property

We should give the absolute Driver class name as input to the system property as follows;

Fig: output for the above program

JDBC 4.0 Auto Loading of the JDBC Driver: 
      Recently SunMicroSystems has released JDBC 4.0 specifications, in which it has specified the DriverManager class which automatically registers the driver. Oracle has implemented this specification and released "ojdbc6.jar". By setting the CLASSPATH to ojdbc6.jar, we can register the driver without writing the code. The following shows you the procedure.

program with no code to register drive
Fig: program with no code to register driver

program with no code to register drive
Fig: output

Hence, we have seen all the major types of registering the JDBC DRIVER.

No comments:

Post a Comment