Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Could not locate DB driver

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I had sorted this little problem out on a previous program - but here it is again!(different program / different code):
I'm getting an exception thrown saying:
Could not locate DB driver
package com.wrox.databases;
import java.sql.*;
import java.util.*;
public class Books {
String error;
Connection con;
public Books() { }
public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
Driver mySqlDriver = (Driver) (Class.forName("com.mysql.jdbc.Driver").newInstance());
DriverManager.registerDriver(mySqlDriver);
con = DriverManager.getConnection(
"jdbc:mysql://localhost/Wrox ?user=root&password=booboomimi");
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception: An unknown error occurred while connecting " +
"to database.";
throw new Exception(error);
}
}
public void disconnect() throws SQLException {
try {
if ( con != null ) {
con.close();
}
} catch (SQLException sqle) {
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}
public ResultSet viewBooks() throws SQLException, Exception {
ResultSet rs = null;
try {
String queryString = ("SELECT * FROM Book;");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute the query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while retrieving books.";
throw new Exception(error);
}
return rs;
}
public void addBooks(int id, String title, float price, int cid)
throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement updatebooks;
updatebooks = con.prepareStatement(
"insert into Book values(?, ?, ?, ?);");
updatebooks.setInt(1, id);
updatebooks.setString(2, title);
updatebooks.setInt(3, cid);
updatebooks.setFloat(4, price);
updatebooks.execute();
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
public void removeBooks(String [] pkeys) throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement delete;
delete = con.prepareStatement("DELETE FROM Book WHERE Title_ID=?;");
for (int i = 0; i < pkeys.length; i++) {
delete.setInt(1, Integer.parseInt(pkeys[i]));
delete.execute();
}
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while deleting books.";
throw new Exception(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
}
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've ammended the code by using a different driver, but I'm now getting this exception being thrown:
java.sql.SQLException: SQLException: Could not connect to database.
This is part of my ammended code:
package com.wrox.databases;
import java.sql.*;
import java.util.*;
public class Books {

String error;
Connection con;
public Books() { }
public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
System.setProperty("jdbc.drivers", "sun.jdbc.odbc.JdbcOdbcDriver");
Driver mySqlDriver = (Driver)(Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance());
DriverManager.registerDriver(mySqlDriver);
con = DriverManager.getConnection(
"jdbc dbc://localhost/wrox?user=root&password=booboomimi";);
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception: An unknown error occurred while connecting " +
"to database.";
throw new Exception(error);
}
}
I noticed when posting this that a red face appears where there should be a colon in the original program - odd.
Any suggestions as where I'm going wrong?
[ December 15, 2002: Message edited by: Rob Petterson ]
[ December 15, 2002: Message edited by: Rob Petterson ]
[ December 15, 2002: Message edited by: Rob Petterson ]
[ December 15, 2002: Message edited by: Rob Petterson ]
 
Everybody's invited. Except this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic