Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DriverManager's Intuition ?

 
Dave Van Even
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");
How does the java.sql.DriverManager know to use the "sun.jdbc.odbc.JdbcOdbcDriver" class as driver ?

thanks
[ June 06, 2002: Message edited by: Dave Van Even ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is:
Class.forName() causes a Class to be loaded
when a Class is loaded, it can execute code (in static blocks)
When a driver is loaded, it registers itself with the DriverManager so that it can be found.
Pretty sneaky, but it allows you to write some pretty good JDBC code.
Dave
 
Dave Van Even
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe thanks
I was thinking about this would be the way it works !
Shouldn't it have been better when you instantiated a driver object yourself and fed it into the driver manager manually ?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not if you want to 'decouple' the Driver from the code.
eg this way allows you to store the driver name as a property (ie a String) then load a Driver at runtime. As long as the SQL is compatable, you can then swap databases at the backend without anyone knowing!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic