This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Is there a difference in the following two ways of loading a JDBC driver? 1. Class.forName("aJDBCDriver"); 2. DriverManager.registerDriver(new aJDBCDriver()); Thanks. Bruce
BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design
Class.forName() explicitly loads the driver class. DriverManager.registerDriver() should be called automatically by the driver when it's loaded. It registers the driver with the DriverManager. The recommended way is the first one.
Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Originally posted by daya mukiri: Hi Bosun, can you explain that some what deeply? does drivermanager internally uses class.forname to load the driver?
No. In Java, classes are almost always loaded implicitly, when they're referenced, and not before. When DriverManager.registerDriver(new aJDBCDriver()); is called, "new aJDBCDriver()" loads the driver (and if the driver conforms to the JDBC specification, loading the driver causes it to call DriverManager.registerDriver() upon itself!! ).
Explicitly loading a class with class.forname is a very rare thing, used to select a class at run time, instead of at compile time. It is used in JDBC so that people can write code that will work with any database driver.