aspose file tools*
The moose likes JDBC and the fly likes what happens if two drivers are loaded? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "what happens if two drivers are loaded?" Watch "what happens if two drivers are loaded?" New topic
Author

what happens if two drivers are loaded?

Veeresh Hosur
Ranch Hand

Joined: Dec 18, 2007
Posts: 32
Hi All,

consider the scenario

Class.forName('oracleDriver');
Class.forName("mysqlDriver");
Connection con = DriverManager.getConnetion("oralcDBURL","UN",PW);

does this work? if yes please expain.

Thanks
Veeresh
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yes.

The DriverManager stores a list of Drivers, and when a request is made for a Driver, the DriverManager asks each Driver if it can handle the DB URL. The first Driver that says 'yes' is asked to provide the Connection.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Drivers have distinct URL syntax. When you call DriverManager.getConnection() the DriverManager looks through its list of registered drivers for one that can service a URL with the syntax you pass. In the example you have, assuming
"oralcDBURL" were a valid Oracle driver syntax, the connection would come from the Oracle driver, the MySQL one is ignored.

[Och! Didn't type fast enough ]
[ April 16, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The DriverManager source code is available with the JDK, see also the API, such as java.sql.Driver.acceptsURL(String url)
Veeresh Hosur
Ranch Hand

Joined: Dec 18, 2007
Posts: 32
Paul Sturrock is it like this?

Connection is the interface present in the JDBC API. DriverManager.getConnection("OracleDBURl") will return connection object which is a vendow specific implementation. Using that connection object we can work on database using JDBC API.

Thanks
Veeresh
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what happens if two drivers are loaded?
 
Similar Threads
An JDBC problem
what this method do in JDBC?
Dynamically loading a class
How to connect MySQL DB from JSP?
JDBC connectivity