Explain Java takes care of registering the driver
By Ramakrishna on Oct 4, 2008 in Java Class Registering
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());
}
}
