4

While doing a simple JDBC connection, all the resources gives the same code that

String driver = "com.mysql.jdbc.Driver";
Statement statement = null; 
Class.forName(driver); 
Connection conn  = DriverManager.getConnection(url + dbName,userName, password);

But we actually nothing do with "Class.forName(driver)". We didn't stored it anywhere. What is the use of that as we nothing do with Class.forName(driver)'s return.

Selvaraj
  • 714
  • 2
  • 6
  • 14
  • 6
    You can read this posts: [JDBC Class.forName vs DriverManager.registerDriver](http://stackoverflow.com/questions/5484227/jdbc-class-forname-vs-drivermanager-registerdriver) and [how does Class.forName() work](http://stackoverflow.com/questions/4202252/how-does-class-forname-work) – Alex K Aug 05 '13 at 12:58

6 Answers6

6

Class.forName() attempts to load the named class. In early versions of JDBC, this was necessary as the Driver class required the class to be loaded in this way. This hasn't been required for ages.

Leave out the call and nothing bad will happen.

For some reason, tutorials and examples persist with the old way.

The only tiny benefit of loading the class manually is that it tells you exactly what the problem is in case you haven't got the right class in the classpath.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

This is the part of com.mysql.jdbc.Driver which registers itself with DriverManager

//
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

It should be noted that since ver 1.6 there is no need to explictly load JDBC drivers using Class.forName(), DriverManager can detect JDBC 4.0 Drivers automatically using Service Provider mechanism. JDBC drivers class name is writen in META-INF/services/java.sql.Driver file

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

The class.forName() causes the ClassLoader to load the class into memory. JDBC driver classes contain a static initializer block that registers the driver with DriverManager for later reference. When you connect DriverManager uses the database parameter to look up the right driver

PSR
  • 39,804
  • 41
  • 111
  • 151
0

Class.forName("driver.class"); loads the specified JDBC driver. When the driver loads, it also registers itself with the DriverManager. Hence, when you call DriverManager#getConnection() you're able to establish the Connection through the driver loaded before.

DriverManager#getConnection()

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

Using Class.forName(..) loads the class. Most java.sql.Driver implementations (using a static initializer) register themselves with the java.sql.DriverManager implementation when the class is loaded. See section 9.2 in the JDBC 4.1 specification for details:

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,

After registration you can create a connection using that driver.

However starting with JDBC 4.0 (Java 6), drivers compliant with the JDBC 4.0 specification no longer need to be loaded this way, as the DriverManager itself will take care of locating and loading JDBC drivers using the ServiceLoader mechanism. See section 9.2.1 of the JDBC 4.1 specification:

The DriverManager.getConnection method has 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 driver’s implementation of java.sql.Driver.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

Well, it does have one side effect - it loads the driver specified by a string name into the memory.

In Java the classes are loaded only when they're actually needed to be used.

So Class.forName() will cause the class loader to "read" the bytecode and load up the class definition into the memory of the JVM.

Now when this happens, the static initialization block of this class (and Drivers should have one) is executed (it should be static because we don't really create objects of this class).

This static initialization block written so that it registers the driver in the DriverManager.

This is a 'by the book' explanation. Of course this API is not that clear and not obvious. Its possible to do this explicitly:

Driver driver = (Driver)Class.forName("com.mysql.jdbc.Drivercom.mysql.jdbc.Driver").newInstance();
DriverManager.registerDriver(driver);

Since Java 6 this mechanism should not be used anymore. Read here about the new way to load up the driver.

Hope this helps

Community
  • 1
  • 1
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97