21

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?

j0k
  • 22,600
  • 28
  • 79
  • 90

5 Answers5

26

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();.

chossenger
  • 613
  • 6
  • 15
  • No problem, the Class/class stuff can be a bit hard to wrap your head around. :) – chossenger Mar 13 '13 at 08:05
  • I 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. – user1884155 Apr 21 '19 at 19:53
9

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().

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
7

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.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • what does the forName method do? –  Feb 23 '13 at 10:41
  • A call to forName("com.mysql.jdbc.Driver") causes the class named Driver to be initialized. – aleroot Feb 23 '13 at 10:48
  • i 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 –  Feb 23 '13 at 11:06
  • @javaL take a look here : http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html – aleroot Feb 23 '13 at 11:51
  • you said to use DriverManager.registerDriver.Can you please post the full code –  Feb 23 '13 at 12:02
  • @javaL I said that you can even use DriverManager.registerDriver but there is nothing against using Class.forNAme ... – aleroot Feb 23 '13 at 12:17
5

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.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
-1

It create a new instance of the com.mysql.jdbc.Driver class and Register the driver.

So then what happens is you call Class.forName("com.mysql.jdbc.Driver") without 'newInstance()' it returns com.mysql.jdbc.Driver class and register the driver only

Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10