21

I've seen this line in a sample application for using a commercial JDBC driver:

Class.forName("name.of.a.jcdb.driver")

The return value is not used.

What purpose does this line serve?

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
  • Nowadays I'd call this an antipattern and favour something like 'DriverManager.register(JdbcDriver.class)'... – Andreas Dolk Aug 21 '09 at 09:30
  • 2
    using register directly requires you to know the driver class before hand. i'd call that an anti-pattern. having the class as a configuration property (and thus Class.forName) makes far more sense to me. – james Aug 21 '09 at 15:17
  • See http://stackoverflow.com/q/8053095/632951 for more information. – Pacerier Aug 24 '14 at 22:42

5 Answers5

27

It performs a static loading of that class. So anything in the static { } block, will run.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • 4
    Which lets the driver class register itself with the JDBC framework. This is needed to allow JDBC to properly recognize the connection URL you pass in in the later calls. – Michal Aug 21 '09 at 08:21
  • a "static" loading? is it not just loading the class, which starts any static initializer (static block AND static variables/constants)? – user85421 Aug 21 '09 at 12:31
13

Maybe some code snippet will help. This is from Sun's JDBC-ODBC bridge driver,

//--------------------------------------------------------------------
// Static method to be executed when the class is loaded.
//--------------------------------------------------------------------


static
{       
    JdbcOdbcTracer tracer1 = new JdbcOdbcTracer();
    if (tracer1.isTracing ()) {
        tracer1.trace ("JdbcOdbcDriver class loaded");
    }

    JdbcOdbcDriver driver = new JdbcOdbcDriver ();

    // Attempt to register the driver

    try {
        DriverManager.registerDriver (driver);
    }
    catch (SQLException ex) {
        if (tracer1.isTracing ()) {
            tracer1.trace ("Unable to register driver");
        }  
    }
}

the DriverManager.registerDriver() call in a static block is executed whenever the driver is loaded through Class.forName().

This used to be the only way to register the driver. JDBC 4.0 introduced a new service registration mechanism so you don't need to do this anymore with newer JDBC 4.0 compliant drivers.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
5

In your specific example, the JDBC driver class contains a static intializer that registers the driver will the DriverManager.

drac_o
  • 427
  • 5
  • 11
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

This is used in particular for JDBC drivers. The JDBC driver class has a static initializer block that registers the class with the JDBC DriverManager, so that DriverManager knows about the driver when you later open a database connection.

In a newer version of JDBC (JDBC 3.0, I think) this is not necessary anymore, a different mechanism is used by DriverManager to find JDBC drivers.

edit - This page explains in detail how loading a JDBC driver works and how the driver registers itself with the DriverManager (the old way).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    Anybody with a link to a description of the new mechanism? I need something similar. – Thorbjørn Ravn Andersen Aug 21 '09 at 07:52
  • 2
    The javadoc for DriverManager from JDK 6 tells about a couple of methods for the DriverManager to find drivers: http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html – Bombe Aug 21 '09 at 07:58
2

In the case of JDBC drivers the static initializer of the requested class will register the driver with JDBC’s DriverManager so that getting a connection for a driver-specific URL works.

Bombe
  • 81,643
  • 20
  • 123
  • 127