File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selection of Driver for Connection

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody please
 
him jain
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic