*
The moose likes JDBC and the fly likes Selection of Driver for Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Selection of Driver for Connection" Watch "Selection of Driver for Connection" New topic
Author

Selection of Driver for Connection

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
DriverManager class goes on searching the drivers for connecting to the database in the order in which they were registered but the drivers listed in jdbc.drivers property are always registered first. So is it the case that the drivers listed in jdbc.drivers property will always be on priority than the drivers loaded with Class.forName() to be used by DriverManager for connecting to database?


I am slow but sure
MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
Anybody please
him jain
Ranch Hand

Joined: Feb 05, 2010
Posts: 56
Well, i found your qun a bit unclear.
I wrote the code for JDBC : "Class.forName("com.mysql.jdbc.Driver");"
then obviously, it'll try to connect to mysql.


--Himanshu Jain
http://jainhim.blogspot.com/
MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
Thanks him for your reply.
But I read somewhere on documentation available on sun site that, drivers that are registered first with DriverManager class are always loaded first and chosen for connecting to the database. Second statement that I read was drivers listed in jdbc.drivers system property are always registered first. So these two statements together tempting me that drivers listed in jdbc.drivers system property will always be on priority to be chosen for connection to the database irrespective of explicitly loading of drivers using Class.forName.

Again correct me if I have taken the concept in wrong manner.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
him jain wrote:I wrote the code for JDBC : "Class.forName("com.mysql.jdbc.Driver");"
then obviously, it'll try to connect to mysql.

No. That statement loads a class, but it doesn't cause anything else to happen. The driver to be used is selected by examining the DB URL, not by checking which drivers are loaded.

MaheshS Kumbhar wrote:But I read somewhere on documentation available on sun site that, drivers that are registered first with DriverManager class are always loaded first and chosen for connecting to the database. Second statement that I read was drivers listed in jdbc.drivers system property are always registered first. So these two statements together tempting me that drivers listed in jdbc.drivers system property will always be on priority to be chosen for connection to the database irrespective of explicitly loading of drivers using Class.forName.

As I mentioned above, the driver to be used is chosen according to the connection URL; it doesn't matter when or how it was loaded. Obviously, it must be loaded somehow before the connection is made. You can even have different drivers loaded for the same DB server, but since those will have different URLs it's clear which one the code intends to use.

If the question is about what happens if you have multiple version of the same driver (which would thus be registered for the same DB URL), then I'd strongly advise not to do that - it's a bug waiting to happen.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selection of Driver for Connection
 
Similar Threads
Re:Class.forName()
connection without class.forName
What does Class.forName do?
ways in doing database connection?
Regarding Class loading