Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Driver problem

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.sql.*;
class Jdbc1
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// to load driver Type1
System.out.println("Got Driver");
Connection con=DriverManager.getConnection
("jdbc:odbc:mydsn","scott","tiger");
// for connection, mydsn is dsn , java is username, java is password
System.out.println("Got Connection");


Class.forName("oracle.jdbc.driver.OracleDriver");
// Type 4 Driver
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@dbs:1521:orcl","scott","tiger");

System.out.println("Driver Loaded");

System.out.println("Connection received");

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from emp");

while(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));

}
rs.close();
con.close();
stmt.close();

}
}

Q ) If i am using Type1 as well as Type 4 Driver in my java application
which will be loaded first?
With the help of which Driver connection is establish?
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have as many drivers in a program as you like. They're loaded by the Class.forName statement, so whichever of those comes first, gets loaded first. That shouldn't matter, though, because they're independent of each other.

The driver to be used is determined by the JDBC URL used in the getConnection call.
 
reply
    Bookmark Topic Watch Topic
  • New Topic