• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading the driver

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the JDBC forum.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Abhay Katekar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic