当从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、用户和密码都写对了,因为我可以在DB中写。
连接由以下人员建立:
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项目中与EclipseOxygen合作。
数据库是用MYSQLWorkbench 6.0CE创建的。
但我不知道司机怎么了。
最佳答案
我认为你的司机名字不正确
这是我用的司机,试试看
com.mysql.jdbc.Driver驱动程序
也将“H”用本地主机小写
如果上述解决方案不起作用:
1:删除导入com.mysql.cj.jdbc.*
2:使用此驱动程序private static final String DRIVER = "com.mysql.cj.jdbc.Driver";