jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Loading the driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Loading the driver" Watch "Loading the driver" New topic

Loading the driver

Abhay Katekar

Joined: Jan 12, 2006
Posts: 11
A driver is loaded using:
And we obtain the connection using:
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

Joined: Jul 08, 2003
Posts: 24199


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: 15092

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: 15302

Moving to the JDBC forum.

GenRocket - Experts at Building Test Data
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
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://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Abhay Katekar

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?

Don't get me started about those stupid light bulbs.
subject: Loading the driver
It's not a secret anymore!