File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes What is the difference between  Class.forName() and  DriverManager.registerDriver() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the difference between  Class.forName() and  DriverManager.registerDriver() ?" Watch "What is the difference between  Class.forName() and  DriverManager.registerDriver() ?" New topic
Author

What is the difference between Class.forName() and DriverManager.registerDriver() ?

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
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

Crazy Bikes created by m-Power
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
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
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Bosun.
Bruce
daya mukiri
Greenhorn

Joined: Nov 02, 2005
Posts: 7
Hi Bosun,
can you explain that some what deeply?
does drivermanager internally uses class.forname to load the driver?
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between Class.forName() and DriverManager.registerDriver() ?
 
Similar Threads
Use of Class.forName()
class . forName() Vs DriverManager . registerDriver
jdbc
difference between different types of loading the drivers
JDBC Connection