I have the following code to connect to a mysql db :
public static void insertIntoDatabase(String code,String name,String temp,String hum,String del) {
Connection con = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
rs = con.prepareStatement("CREATE TABLE IF NOT EXISTS AiportDetails(code VARCHAR(50) PRIMARY KEY, " +
"name VARCHAR(50), temp VARCHAR(50), hum VARCHAR(50), del VARCHAR(50)) ENGINE=InnoDB;").executeQuery();
rs = con.prepareStatement("INSERT INTO AirportDetails(code,name,temp,hum,del) VALUES("+code+","+
name+","+temp+","+hum+","+del+");").executeQuery();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
I am getting the following error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
NOTE Some common corrections I found online were:
1. The driver is not in the /WEB-INF/lib folder.
2. The url is wrong.
I dont think this is the case with my code.
Thank you.
That can happen if you didn’t load the driver before making the first connection ever.
To be sure, the driver has to go in
/WEB-INF/lib, not in/WEB-INF. You’ve there by the way some SQL injection holes. Look atPreparedStatement. Thefinallycan also be improved, as you have it now, theconwill never be closed whenrs.close()throws an exception.