This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bruce Jin
Ranch Hand
Posts: 672
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bosun.
Bruce
 
daya mukiri
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bosun,
can you explain that some what deeply?
does drivermanager internally uses class.forname to load the driver?
 
stu derby
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic