从 eclipse 连接到数据库时,我不断收到下一个异常。
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at connection.ConnectionFactory.<init>(ConnectionFactory.java:29)
at connection.ConnectionFactory.<clinit>(ConnectionFactory.java:25)
at dao.StudentDAO.insert(StudentDAO.java:53)
at bll.StudentBLL.insertStudent(StudentBLL.java:39)
at start.Start.main(Start.java:23)
然而,连接已建立,我可以从数据库中读取,我可以写入其中但异常并没有消失。 我已将 mysql-connector-java-5.1.45-bin 添加到具有构建路径的路径文件中,并出现在库部分。 我相信 URL、用户和密码都写对了,因为我可以在数据库中写。 连接建立于:
try {
connection = DriverManager.getConnection("jdbc:mysql://localHost:3306/schooldb","root","");
} catch (SQLException e) {
LOGGER.log(Level.WARNING, "An error occured while trying to connect to the database");
e.printStackTrace();
}
return connection;
}
驱动程序的代码是:
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
我正在 Maven 项目中使用 eclipse Oxygen。 数据库是用 MYSQLWorkbench 6.0CE 创建的。
但我不知道驱动程序有什么问题。
最佳答案
我认为您的驱动程序名称不正确 这是我用的驱动,试试看
com.mysql.jdbc.Driver
也将本地主机中的“H”设为小写
如果上述解决方案不起作用:
1: 删除导入 com.mysql.cj.jdbc.*
2:使用这个驱动private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
关于java - 未找到驱动程序异常 JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50114388/