Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what happens if two drivers are loaded?

 
Veeresh Hosur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic