What purpose does Class.forName() serve if you don't use the return value?

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


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.


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