Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading a JDBC Driver

 
Siddharth Jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siddharth,
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dave
 
Siddharth Jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic