wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes DriverManager's Intuition ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DriverManager Watch "DriverManager New topic
Author

DriverManager's Intuition ?

Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jul 19, 2001
Posts: 101
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

Joined: Mar 06, 2001
Posts: 13459

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!
 
 
subject: DriverManager's Intuition ?
 
Similar Threads
classnotfoundexception
Making Connection with database
which is more faster for sql2k server?
An JDBC problem
JDBC connectivity