File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How my program runs without registering driver? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How my program runs without registering driver?" Watch "How my program runs without registering driver?" New topic
Author

How my program runs without registering driver?

Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
Hi friend,
I understand that from my program I can register a JDBC driver by using the Class.forname(driver) method.
I tested this method with two DBMSs (Derby and Access). What I did is as follows:
First I used Class.forname(driver) method and the program ran successfully and fetched data from the database.
Next time I commented the forname(driver) method (as well as the corresponding ClassNotFoundException) and then also the program ran successfully!!!

So, my question is how the program could fetch the data when at the second time I did not register the JDBC driver?? Please tell me. I am new to it.


Always say the TRUTH only
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

JDBC 4.0 uses the ServiceLoader mechanism besides manually loading classes. Perhaps your driver is JDBC 4.0 compliant. Check out the Javadoc of DriverManager for more information.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
I am sorry, but it is not clear to me.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you read the DriverManager documentation?

(I hadn't until now, and didn't know this capability existed, so thanks! I don't do much that requires this, but it's still interesting.)
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
I have read it. It says,

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.


I just can't understand why applications no longer need to explicitly load JDBC drivers? How java finds it out? What does it mean by drivers loaded at initialization?

Please help me.
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
I think I got it. But partly it is still unclear.
I got the fact that ServiceLoader mechanism needs the driver jar file to keep a file named java.sql.Driver in the META-INF\services folder and the content of the file should be name of the driver class that java automatically loads at the run time.
I was trying the Derby database. So looked at its java.sql.Driver file (in the derby.jar) and its content is:


What I did not get is that if org.apache.derby.jdbc.AutoloadedDriver is being loaded if I don't manually load any driver, how the driver org.apache.derby.jdbc.EmbeddedDriver gets loaded automatically if I don't use the forName() method?
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
Is there somebody to help me? Please?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

DriverManager asks for all implementations of java.sql.Driver. The ServiceLoader mechanism will look at all META-INF/services/java.sql.Driver resources. This means it searches all JAR files on the class path for files with this name. It will try to instantiate the classes inside these files. This will cause the SQL drivers to register themselves at DriverManager.
 
jQuery in Action, 2nd edition
 
subject: How my program runs without registering driver?
 
Similar Threads
odbc question
load and register driver
confirm JDBC Driver registration
how to register a driver
Class.forName