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 ]
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
Joined: Jul 19, 2001
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 ?
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!