what exactly does this do Class.forName("com.mysql.jdbc.Driver").newInstance();
Solution 1
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();
.
Solution 2
Quoting from the JDBC Specification, Chapter 9, Section 2:
JDBC drivers must implement the Driver interface, and the implementation must contain a static initializer that will be called when the driver is loaded. This initializer registers a new instance of itself with the DriverManager.
And an example code is provided for AcmeJdbcDriver
as follows:
public class AcmeJdbcDriver implements java.sql.Driver {
static {
java.sql.DriverManager.registerDriver(newAcmeJdbcDriver());
}
}
And when you call Class.forName(String className), according to the API Documentation, the following happens:
A call to forName("X") causes the class named X to be initialized.
where initialization involves code in static block to be executed.
So basically, you initialize the Driver class, and in turn the class registers itself with the java.sql.DriverManager
per the JDBC specification.
Please note, this is not needed anymore. Details can be found here.
The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:
my.sql.Driver
Applications no longer need to explictly load JDBC drivers using Class.forName().
Solution 3
It initialize the class "com.mysql.jdbc.Driver"
if found in the classpath, this imply that the driver is registered in the JDBC driver manager since the registration process is inside the static initializer of the driver class ...
There is another approach you can use to register a driver : is to use the static DriverManager.registerDriver()
method.
Solution 4
It will create a new instance of the com.mysql.jdbc.Driver
class and hence call the static initialization which will register the driver with the DriverManager
so you can create mysql connections based on the URL you use in the second line.
The class however should be in the mysql.jar
.
Related videos on Youtube
j0k
French symfony developer since few years. There was a time I posted some post here: http://www.j0k3r.net After a year of travel, I wrote my trip on this blog: https://wildtrip.blog Say me "Hi !" on Twitter: @j0k :-)
Updated on August 01, 2020Comments
-
j0k almost 4 years
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?
-
Admin about 11 yearswhat does the forName method do?
-
aleroot about 11 yearsA call to forName("com.mysql.jdbc.Driver") causes the class named Driver to be initialized.
-
Admin about 11 yearsi have another question,actually i have forgotten my mysql password and i have checked all the answer in SO about changing mysql password but i could not retrieve it. Can you please help me in this
-
aleroot about 11 years@javaL take a look here : dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
-
Admin about 11 yearsyou said to use DriverManager.registerDriver.Can you please post the full code
-
aleroot about 11 years@javaL I said that you can even use DriverManager.registerDriver but there is nothing against using Class.forNAme ...
-
chossenger about 11 yearsNo problem, the Class/class stuff can be a bit hard to wrap your head around. :)
-
Koray Tugay over 7 yearsYou do not need to create an instance to run the static block.
-
Aleksandar almost 7 yearsThis is a much better explanation! Thank You!
-
Brent Sandstrom over 6 yearsupped for being the only one who mentions that apps don't need to call Class.forName(..) anymore
-
user1884155 about 5 yearsI downvoted because either though this is technically correct, it does not help in anyway in understanding why this code is used in this the context of using jdbc connections. It also fails to mention this line of code is no longer necessary. The answer of Koray makes more sense and is more up to date.