In JDBC, the way to get connection to a database is:
Class.forName("<name of driver>");
DriverManager.getConnection("url") ;
Lets analyse these two lines.
The first line class.forName(), simply finds that class from the class path and loads it.
I mean, nothing stops you from loading the driver class by including it in the import statements. The code will work fine. But we dont put it in the import statements as we want to keep the flexibility of changing the driver withing recompiling our code.
There is something more. The driver classes have a static block that run as soon as the class is loaded.
The static block registers that driver with the driverManager against a perticular subprotocol. For example a driver called com.test.XYZDriver might register itself against the subprotocol "XYZ"
Now about the second statement: Drivemanager.getConnection("url");
A database URL (or JDBC URL) is a platform independent way of adressing a database. A database/JDBC URL is of the form jdbc:[subprotocol]:[node]/[databaseName]
When the static method "getConnection(url)" is executed, DriverManager extracts the subprotocol from the url and finds the driver registered against that subprotocal. For a url "jdbc:XYZ:192.168.23.34/myDB", the driverManager will try to find the driver registered against the subprotocol "XYZ". The driver is expected to know how to make sence of the rest of the protocol and get the connection. The driverManager returns that connection.
http://www.entrepreneurship.net.tw/epoch_Forum/PHP/viewtopic.php?p=6863
Comment by amiee — September 29, 2007 @ 10:58 pm
good exaplanation
Comment by sandhya — December 26, 2007 @ 11:44 am