This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Loading a JDBC Driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Loading a JDBC Driver" Watch "Loading a JDBC Driver" New topic

Loading a JDBC Driver

Siddharth Jain

Joined: Jun 13, 2003
Posts: 4
Can we use the fully qualified class name of the driver alongwith the new operator to load a driver instead of using Class.forName()?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Are you sure that the driver class has a no-arg constructor...? I'd say it's better to say Class.forName("com.foobar.Driver")
Why don't you want to use Class.forName?

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to conform to the JavaRanch naming policy.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The problem with using new my.DbDriver() versus Class.forName("my.DbDriver") is that sometimes it encourages people to try to manually register the Driver with the DriverManager, and this is the wrong thing to do.
If you do that then it is possible to register a Driver twice. This may not be an immediate problem, but if you try to deregister the Driver, one copy will still be held by the DriverManager. Unlikely, but why risk it if you don't have to.
I find it's better to find a template for JDBC operations to follow. This includes using Class.forName(...) then getting all Connections from the DriverManager.
Siddharth Jain

Joined: Jun 13, 2003
Posts: 4
thanks dave, i know that using the Class.forName() is the right way but I was just asking whether it was possible? From your reply I guess it is.
Consider Paul's rocket mass heater.
subject: Loading a JDBC Driver