This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DriverManager class

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a new learner of JDBC and I read two statements about DriverManager class.
1. While using DriverManager class for connection to database, requires an application to load a specific driver, using a hardcoded URL. and
2. As part of initialization of DriverManager class, the DriverManager class attempts to load the driver classes referenced in the jdbc.drivers system property.

It seemed to be two contradictory statements to me for loading of driver classes

Please guide me if I am wrong.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's no contradiction. If your app wants to use JDBC, then the driver for your particular DB needs to be loaded; that's what #1 is saying. #2 is saying that there's a default way to load drivers without the app having to do anything about it. But that doesn't change the fact that the right driver needs to be loaded somehow.

Maybe the confusion is over the "requires an application" in #1. If by "application" you understand just the code you wrote, then that statement is not correct due to #2. If by "application" you understand the JVM running your code, then it is correct, because JDBC performs #2 behind your code's back (but it is documented in the DriverManager javadocs).
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for your input. But if there's a default way of loading the driver via jdbc.drivers system property then why to go for manually loading of driver using Class.forName, or is there any uncertainty about loading of driver with default behaviour of jdbc.drivers system property?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, in more than a dozen years of working with JDBC I have never encountered a situation where that property was used. IMO, messing around with system properties is not a good way of doing this - it's not very flexible, and potentially troublesome in environments where multiple applications run in the same JVM (like in app servers and servlet containers).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic