It's not a secret anymore!*
The moose likes JDBC and the fly likes Class.forname and DriverManager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Class.forname and DriverManager" Watch "Class.forname and DriverManager" New topic
Author

Class.forname and DriverManager

Hs Raveendran
Greenhorn

Joined: Feb 05, 2010
Posts: 23





How does DriverManager establishes the connection with the DB
i.e. DriverManager is from java.sql package how does it get the services from the above loaded driver class file

Can any one please tell me in detail.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

When a Driver class file is loaded by the ClassLoader, it is required to register itself with the DriverManager (using a static initialisation block)
This code block is ony called once, so it is OK to call Class.forName() multiple times without too much concern.
When the DriverManager is given a JDBC URL, it asks each of the Drivers that it knows whether they are able to accept a URL in that format. If the Driver says yes then the DriverManager asks it to create a connection which is then returned to the calling code.
Hs Raveendran
Greenhorn

Joined: Feb 05, 2010
Posts: 23

ClassLoader, is required to register itself with the DriverManager (using a static initialisation block) .

I not able to understand this part. register means what?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

register means it must cal DriverManager.registerDriver() to tell the DriverManager that this Driver is available.
static initialisation is a section in a class that looks like this which gets executed once when the Class is loaded:

If you cause the class to be loaded then the message will be displayed:
 
wood burning stoves
 
subject: Class.forname and DriverManager
 
Similar Threads
Loading the driver
DriverManager lookup
Class.forName in depth
jdbc with microsoft SQl Server 2008
loading driver