Issue
While connecting to MySQL database I do following steps
Connection con = null;
Resultset rs = null;
Statement st = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","passwp");
Actually I wanted to know what does Class.forName("com.mysql.jdbc.Driver").newInstance();
statement do.
Althogh this class is not in mysql.jar. Where is it present?
Solution
The Class
class is located in the java.lang package, so it is distributed with java, and imported automatically into every class.
What the forName()
method does, is just return the Class
object for the paramater that was loaded by the class loader. The newInstance()
method then returns a new instance of the class.
So then what happens is you call
Class.forName(...)
it returns com.mysql.jdbc.Driver.class.
You then call newInstance()
on that class which returns an instance of the class, whith no paramaters, so it's basically calling new com.mysql.jdbc.Driver();
.
Answered By - chossenger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.