我试图通过jdbc连接MySQL数据库,但代码抛出错误“java.lang.ClassNotFoundException:com.MySQL.jdbc.Driver”
霉菌代码:
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。司机
我曾尝试从系统环境变量向类路径添加以下行
“C:\ProgramFiles(x86)\MySQL\Connector J5.1.20.0\MySQL-Connector-java-5.1.20-bin.jar”
但还是不行。我也犯了同样的错误,有人能想出并提出解决方案吗
# 1 楼答案
我怀疑你的课程设置是否正确
要验证这一点,请在^{开头添加这一行:
并验证
mysql-connector-java-5.1.20-bin.jar
是否正确显示在该行中# 2 楼答案
对不起,这是我的错
我忘了在我正在使用的eclipse中添加mysql jar文件。它运转良好