当从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)
但是,连接建立后,我可以从数据库中读取数据,也可以在其中写入数据,但异常不会消失。我已经用build path将mysql-connector-java-5.1.45-bin添加到路径文件中,并出现在library部分。我相信网址,用户和密码写得正确,因为我可以写在数据库。通过以下方式建立连接:
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创建的。
但我不知道司机怎么了。
2条答案
按热度按时间f0ofjuux1#
问题是您使用的是mysql connector/j5.1.45驱动程序,它使用驱动程序类名
com.mysql.jdbc.Driver
,但请尝试加载新的mysql connector/j8.x驱动程序中引入的驱动程序类名。升级至mysql connector/j 8.0.11(可在https://dev.mysql.com/downloads/connector/j/),或者-如果您现在想继续使用5.1.45-请使用
另外,为了向后兼容,mysql connector/j8.x还保留了上述类以供加载。
从技术上讲,由于JDBC4和更高的自动驱动程序加载,在很多情况下这样加载驱动程序甚至不是必需的。
zaqlnxep2#
我想你的司机名字不对这是我用的司机,试试看
com.mysql.jdbc.driver驱动程序
同时将本地主机中的“h”改为小写
如果上述解决方案不起作用:
1:删除导入
com.mysql.cj.jdbc.*
2:使用这个驱动程序private static final String DRIVER = "com.mysql.cj.jdbc.Driver";