• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Drivers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been assigned a project in which I am to connect using 3 different drivers for 3 different databases. For this assignment, I've built a GUI with a JCombo box. When a choice is made from the combo box, a properties file is chosen, the driver is loaded and registered, and metadata is obtained from the database.
The 3 databases are MSAccess, MySQL, and Pointbase (from the Core Java book by Horstmann). When I choose from the combo box, the first choice works fine. It doesn't matter which database/driver combination I choose, they all work as long as it is the FIRST choice. Any subsequent choices from the combo box result in an SQLException (no suitable driver).
My question is: Do I have to close the connection and make a new connection to load another driver? If so, how do I test for an open connection and close the currently open one?
OR
Do I have to un-register drivers before registering a new one? And is this true even if each connection is a new, distinct object (a separate connection object for each database). I tried making a separate connection object for each database depending on the combo box choice, but I received the same SQLException (no suitable driver).
Thanks
------------------
Jim
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to your question... the answer is no.
Are you doing a Class.forName() for each driver? Show us some code because this should not be a problem.
 
Jim Robinson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were right. Using Class.forName() instead of trying to set the system property solved the problem. Thanks!

------------------
Jim
[This message has been edited by Jim Robinson (edited March 17, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Jim Robinson
can u provide me the souce code of the 3-DATABASE Connection.
i'm having same proj. but i facing probs.ur code will help me a lot. thanks
mayur
 
Jim Robinson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Make a Connection and create Statement and DatabaseMetaData objects
public void getDatabaseInformation(String fileName){

try {
con = getConnection(fileName);
stmt = con.createStatement();
md = con.getMetaData();
}
catch(SQLException e) {
while(e != null) {
System.out.println("Error closing connection in tab switch");
System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Vendor Code: " + e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
catch(IOException e) {
System.out.println("Error in I/O operation: " + e);
}
}


// Makes the connection to the database
public static Connection getConnection(String fileName) throws SQLException, IOException {
Properties props = new Properties();
FileInputStream in = new FileInputStream(fileName);
props.load(in);

// force the driver to load with Class.forName()
try {
Class.forName(props.getProperty("jdbc.drivers"));
}
catch(ClassNotFoundException e){
System.out.println("Exception loading driver: " + e);
}

String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url);
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic