Monday, 24 October 2016

Register the Driver

Register the Driver

There are two ways to register the JDBC driver,
  1. Using the forName() method.
  2. Using the registerDriver() method.
forName() method
  • The forName() method is exists in java.lang.Class class.
  • Its a static method, so we need to call this method by using class name
  • This method will helpful to register the jdbc driver
  • This method loads the Driver class dynamically.
Syntax
public static void forName(String className)throws ClassNotFoundException
Example
Class.forName("oracle.jdbc.driver.OracleDriver");
-------------------------------------------------------------------------------------
Program         :       Demo to register the database
Program name  :      DBRegister1.java
Output             :         
 Driver is Registered successfully

class DBRegister1
{
public static void main(String args[])throws ClassNotFoundException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver is Registered successfully");
}

}

Note:
To compile above java program we have to set the class path to ojdbc14.jar, so, keep this jar in corresponding directory then set the class path.
Set class path:
  • C:\Nireekshan\JDBC>set classpath=ojdbc14.jar;.;
Compile      :          javac DBRegister1.java
Run            :          java DBRegister1
Output        :         
Driver is Registered successfully
-------------------------------------------------------------------------------------
registerDriver() method
  • The registerDriver() method is exists in java.sql.DriverManager class.
  • Its a static method, so we need to call this method by using class name.
  • This method will helpful to register the jdbc driver
Syntax
             public static void registerDriver(Driver driver)
  • Very first step we need to create object to Driver to load a JDBC driver.
  • The created driver object we need to pass as parameter to registerDriver() method.
  • In second step we need to call registerDriver() method.
Syntax 
                    Driver driver = new <DriverName>();


Example
               DriverManager.registerDriver(driver);
-------------------------------------------------------------------------------------
Program         :       Demo to register the database
Program name  :      DBRegister2.java
Output             :         
 Driver is Registered successfully

import java.sql.*;
class DBRegister2
{
public static void main(String args[])throws SQLException
{
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
System.out.println("Driver is Registered successfully");
}

}

Note:
To compile above java program we have to set the class path to ojdbc14.jar, so, keep this jar in corresponding directory then set the class path.
Set class path:
  • C:\Nireekshan\JDBC>set classpath=ojdbc14.jar;.;
Compile      :          javac DBRegister2.java
Run            :          java DBRegister2
Output        :         
Driver is Registered successfully
-------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan