aspose file tools*
The moose likes JDBC and the fly likes Loading the driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Loading the driver" Watch "Loading the driver" New topic
Author

Loading the driver

Abhay Katekar
Greenhorn

Joined: Jan 12, 2006
Posts: 11
A driver is loaded using:
Class.forName();
And we obtain the connection using:
DriverManager.getConnection();
Now the question is how does the DriverManager class now which driver to use to get the connection.
This is what a few people asked me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between.

Please go here and fix your display name up, pronto. Thanks, pardner!


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Hello Abby, please change your display name as Ernest asks you!

When you load the driver class with Class.forName("..."), the JDBC driver registers itself with the DriverManager. By doing this, it tells the DriverManager which kind of JDBC URL this driver is for.

When you ask the DriverManager for a connection by calling getConnection(...), the DriverManager uses the JDBC URL that you supply to look in its registry of drivers, and picks a driver that can handle that URL.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Moving to the JDBC forum.


GenRocket - Experts at Building Test Data
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
When you call Class.forName the class that you pass to it registers itself with the DriverManager. Then when DriverManager.getConnection is called the DriverManager iterates through all registered drivers and queries them if they recognize the passed in URL. If the driver accepts the URL its getConnection method is called.

Here is an implementation of a JDBC driver class I created for my JAMon Driver. JAMon responds to any jdbc url that starts with 'jdbc:jamon:'. JAMon is simply a proxy driver that wraps other drivers to track performance, but other drivers would work the same way.

http://jamonapi.cvs.sourceforge.net/jamonapi/jamonapi/src/java/com/jamonapi/proxy/JAMonDriver.java?revision=1.9&view=markup


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Abhay Katekar
Greenhorn

Joined: Jan 12, 2006
Posts: 11
Thanks Steve,
That clears my doubt drivers.
But then again if I load multiple drivers one after another using multiple Class.forname() statements, which driver will get used? Or will the DriverManager pick the first suitable one? Also if any other section of the program has loaded a driver previously(which is not a good practice) could that driver get used.
So is there any way I could force it to use a specific driver?

Regards,
Abhay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading the driver