Wednesday, July 31, 2013

How many ways are there to register JDBC driver with DriverManagerService

                                                JDBC is used for to interact java application with Data Base Software.By using JDBC API database vendors are develop JDBC drivers.This JDBC driver class names are vary from Database to Database. This JDBC driver class name contains static method which contains logic to object for JDBC driver class and register this object with DriverManager Service. Generally we are using this process to register Driver class with DriverManager Service.But we can register our Driver  class with DriverManager Service in so many ways.They are
1. 
    Sun.jdbc.odbc.JdbcOdbcDriver obj=new Sun.jdbc.odbc.JdbcOdbcDriver();
    DriverManager.registerDriver(obj);
           This above code register JDBC driver  twice with DriverManager Service.
 (i)To static block of driver class when driver class object is created.
 (ii) Explicitly when programmer calls DriverManager.registerDriver();

2.
     Sun.jdbc.odbc.JdbcOdbcDriver obj=new Sun.jdbc.odbc.JdbcOdbcDriver();
            Here JDBC driver will be register DriverManager Service only once but programmer creates one explicit unused object for driver class.

3. DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());

    Limitations are same as approach 1

4. class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");

static block of driver class registers jdbc driver with DriverManager service only once.

5. class.forName(args[0]);
   >java javaclassname Sun.jdbc.odbc.JdbcOdbcDriver

Same as approach 4 but allows to pass JDBC driver class  name from outside the application dynamically at runtime. 

6. We can pass input value to Java application as System property value by using java -D option.To read this value our application can use System.getProperty() method.

>java _Dmy.driver=Sun.jdbc.odbc.JdbcOdbcDriver() javaclassname

7. The DriverManager class attempts to load the Driver classes automatically which are referenced in the System property called "jdbc.driver"
             
  >java _Djdbc.drivers=Sun.jdbc.odbc.JdbcOdbcDriver() javaclassname   
8. In the process of loading subclass the JVM automatically loads superclass will be executed automatically .The below process is not recommended it makes our class not extending from other useful classes like Thread,Frame,Applet.

No comments:

Post a Comment