Could someone explain to me where I’m going wrong with the following code:
package newdbtet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewDBTet {
public static void main(String[] args) throws InstantiationException, SQLException, IllegalAccessException {
try {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "evidence_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database");
conn.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(NewDBTet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Exception error:
Jul 16, 2012 2:59:24 PM newdbtet.NewDBTet main
SEVERE: null
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Does this mean that I haven’t installed the driver / library correctly? Sorry – not the best with Java.
download the MySQL Driver for Eclipse/Java then you should get a .jar driver. then right click on your class and go to build path. finally add the external library to your project, that should solve your problem.