11

I am told that the prefered method to load the JDBC driver is :

Class.forName(driverName);

I understand that this is better for a dynamic decision between multiple drivers maybe read from an XML config file or user input. The thing I am curious about is how does invoking this statement loads the stated driver into the environment where we are not even storing the resultant "Class" object anywhere. The JavaDocs entry says:

public static Class forName(String className)
                 throws ClassNotFoundExceptionReturns 

returns the Class object associated with the class or interface with the given string name

In that case, how do the Java developers managed to facilitate the existence of driver object with merely this statement?

jzd
  • 23,473
  • 9
  • 54
  • 76
mihsathe
  • 8,904
  • 12
  • 38
  • 54
  • 3
    Note that the `Class.forName(...)` is not necessary anymore if you're using JDBC 4.0 or newer (included in Java SE 6). JDBC can automatically find and initialize drivers now. See: http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html – Jesper May 13 '11 at 13:38
  • More info at http://stackoverflow.com/a/8053125/632951 – Pacerier Aug 24 '14 at 22:45

1 Answers1

28

The Class#forName() runs the static initializers (you know, static applies to the class, not to the instance). The JDBC driver implementation should register itself in the static initializer.

public class SomeDriver implements Driver {

    static {
        DriverManager.registerDriver(new SomeDriver());
    }

}

Note that there exist buggy JDBC drivers such as org.gjt.mm.mysql.Driver which incorrectly registers itself inside the constructor instead. That's why you need a newInstance() call afterwards on such drivers to get them to register themselves.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    I got it sir. Thanks. Best answer all the way :) – mihsathe May 13 '11 at 13:08
  • 1
    @BalusC, Why do we need to use Class.forName to run the static initializer? If we use `SomeDriver` directly, e.g. if we have already an instance of it, or we call `SomeDriver.StaticMethod` directly, then wouldn't that already ensured that the static initializer will be called? – Pacerier Aug 24 '14 at 22:24
  • 2
    @Pacerier: Because you don't want to have a dependency on `SomeDriver` (i.e. you don't want to `import` it anywhere in your code). This way your code is portable across various JDBC drivers without the need to change it everytime you (or enduser!) switch driver or DB. Related: http://stackoverflow.com/q/7550612 – BalusC Aug 25 '14 at 05:30
  • @BalusC, I mean if somewhere in the code, it already did `SomeDriver.StaticMethod`, or `new SomeDriver` or `some_driver.Method`, then why do we need to use `Class.forName` to run the static initializer? – Pacerier Aug 25 '14 at 05:44
  • @Pacerier: Your code has a problem. You indeed don't need to explicitly load the class via `Class#forName()` yourself (provided that the class is loaded *before* `getConnection()` call is made). – BalusC Aug 25 '14 at 05:48
  • @BalusC, Ok, but why do you say that the code has a problem? – Pacerier Aug 25 '14 at 07:46