我的代码:
public class database_connection {
public static void main(String[] args) throws SQLException {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mycooldatab";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try{
Class.forName(driver).newInstance();// create object of Driver
conn = DriverManager.getConnection(url+dbName,userName,password);
// connection will be established
// *******************Statement******************
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
// rs.next(); // 1st row
// System.out.println(rs.getString("name"));
} catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
我曾尝试将以下行从系统环境变量添加到类路径。
“C:\Program Files (x86)\MySQL\Connector J 5.1.20.0\mysql-connector-java-5.1.20-bin.jar”
但还是不行。我遇到了同样的错误,有人可以找出并提出解决方案吗?