Explain Java takes care of registering the driver



Q) How Class.forName takes care of registering the drivers?

Rep) (“oracle.jdbc.driver.OracleDriver()”);

The driver vender provides a static block.As part of this block code will be provided to take care of registering the driver.

When Class.forName is executed the class will be loaded and the static block of the class will be executed. The code in the static block takes cares of registering the driver.

We can provide the code as shown below to make the application flexible (work with multiple database)

//dc :- your database connectivity class loading is here

Ex: for Oracle (“oracle:jdbc:driver:OracleDriver()”);

//Url :- your connection Driver with the username and password.

Ex: for Mysql(“jdbc:oracle:thin:@localhost:1521:xe”,”username”,”password”);

//username : your username for using database

//password : your password to that database

public class xxxx {

String Vdc = System.getProperty(“dc”);

String Vurl = System.getProperty(“url”);

String Vun = System.getProperty(“username”);

String Vpswd = System.getProperty(“password”);

{

Class.forName(“Vdl);

Connection con=DriverManager.getConnection(Vurl , Vun , Vpswd);

System.out.println(“——-Connected—–>”con.getClass());

}

}

Random Posts

  • Java Thread Interview Questions
  • Difference between #include and Import
  • EJB INTERVIEW QUESTIONS
  • Difference between session.save() , session.saveOrUpdate() and session.persist()
  • Hibernate Interview Questions

Post a Comment