Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which Driver will be used?

 
an bo
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a case where you have multiple drivers loaded onto your memory....

When you run getConnection(), which of the above mentioned drivers will be used to get the connection?

Thanks in Advance.

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4117
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName method is used for the class loading purpose only. The driver depends on the value you passed to the getConnection method.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

documentation says:
When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.


the Class.forname() simply registers the driver with
the DriverManager.

When calling DriverManager.getConnection() the
implementation iterates over all registered drivers
and tries to connect.
If an Exception is thrown, it continues iterating.

You can have a look in the DriverManager.java
source code.

Your Java installation comes with src.zip that includes
all source code.

Hope that helps
Matt
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is the connection URL passed to the DriverManager.getConnection() or getDriver() methods. Compare the URLs for several different database's JDBC drivers and you will see that even though they all start with "jdbc:", after that they are quite different, with the next word usually identifying the database type. The Driver Manager, when given the connection URL, cycles through the registered JDBC drivers asking each one whether it recognizes the URL. The first driver to say this it does is the one used.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic