During many interviews I have taken , I have observed even seasoned programmers find it difficult to explain the use of class.forname() method while making a JDBC connection object. In this post, I am just trying to explain this,
Here is a simple JDBC code,
import java.sql.*;
public class JdbcConCode {
public static void main(String args[]) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/nilesh";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "nilesh";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
System.out.println("Connection is created....");
//TODO
//rest of the code goes here
} catch (Exception e) {
System.out.println(e);
}
}
}
Let's go through the code above, the url indicates the location of the database schema, here the database is nilesh.
Then there is Driver which is the fully qualified JDBC driver class name.
Other two variables are used to declare the username and passwords to access the database. Just for the purpose of this code and to maintain simplicity ,I have mentioned all these variables inside the class itself, generally, this kind of details are retrived through the properties or xml files.Let's not delve into those details, the actual code starts after the try{ block,
class.forName(String className)
Basically, here the forName() call tries to locate, load and link the driver class to JDBC API. With in java , dynamic loading is acheived by class.forName() and it is the basis of core reflection functionality provided by Java.
Class.forName attempts to load the class whose name is
given in its only argument (driver class), and returns the Class instance representing that class. In the
event that the Class could not be found, resolved, verified, or loaded, Class.forName
throws one of several different Exceptions, all of which are listed in the javadoc page for
java.lang.Class [1].
Back to the code, here the class.forName() is called as,
class.forName("com.mysql.jdbc.Driver");
Using the complete package name , it first locates the class binary in the classpath and then loads that as a object representation in memory.
Once the Driver class is loaded in memory , it's static code block is executed which has something like,
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
That is the Driver gets registered with JDBC api as soon as class.forName() executes. So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.
Since the Driver is registered the next line of code just get us the Connection() class object to work with database. But thats not that straight forward.
We all know that the DriverManager class returns a database connection given a JDBC URL string, a username and a password. However, in order to create that connection, the
DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that are registered with it (ie: the registerDriver(Driver driver) method ) and calls the acceptsURL(url)) method on each driver in the array, effectively asking the driver to tell it whether or not it is compatible to handle the JDBC calls.
So this explains how the class.forName() works for you.
Here is a simple JDBC code,
import java.sql.*;
public class JdbcConCode {
public static void main(String args[]) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/nilesh";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "nilesh";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
System.out.println("Connection is created....");
//TODO
//rest of the code goes here
} catch (Exception e) {
System.out.println(e);
}
}
}
Let's go through the code above, the url indicates the location of the database schema, here the database is nilesh.
Then there is Driver which is the fully qualified JDBC driver class name.
Other two variables are used to declare the username and passwords to access the database. Just for the purpose of this code and to maintain simplicity ,I have mentioned all these variables inside the class itself, generally, this kind of details are retrived through the properties or xml files.Let's not delve into those details, the actual code starts after the try{ block,
class.forName(String className)
Basically, here the forName() call tries to locate, load and link the driver class to JDBC API. With in java , dynamic loading is acheived by class.forName() and it is the basis of core reflection functionality provided by Java.
Class.forName attempts to load the class whose name is
given in its only argument (driver class), and returns the Class instance representing that class. In the
event that the Class could not be found, resolved, verified, or loaded, Class.forName
throws one of several different Exceptions, all of which are listed in the javadoc page for
java.lang.Class [1].
Back to the code, here the class.forName() is called as,
class.forName("com.mysql.jdbc.Driver");
Using the complete package name , it first locates the class binary in the classpath and then loads that as a object representation in memory.
Once the Driver class is loaded in memory , it's static code block is executed which has something like,
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
That is the Driver gets registered with JDBC api as soon as class.forName() executes. So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.
Since the Driver is registered the next line of code just get us the Connection() class object to work with database. But thats not that straight forward.
We all know that the DriverManager class returns a database connection given a JDBC URL string, a username and a password. However, in order to create that connection, the
DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that are registered with it (ie: the registerDriver(Driver driver) method ) and calls the acceptsURL(url)) method on each driver in the array, effectively asking the driver to tell it whether or not it is compatible to handle the JDBC calls.
So this explains how the class.forName() works for you.
Jumanji Slot Review - Jumanji Online Slot Review - JTM Hub
ReplyDeletePlay 여주 출장샵 the 서울특별 출장안마 Jumanji online 양산 출장마사지 slot at Jumanji.com - a brand new online 삼척 출장마사지 casino with great bonuses 창원 출장마사지 and promotions!