gpt4 book ai didi

Java JDBC MySQL连接错误: ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-30 23:02:16 28 4
gpt4 key购买 nike

我正在尝试连接到数据库,但出现此错误。

这是代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JDBCInsertValues {

public static void main(String[] args) throws Exception {

Class.forName("com.mysqljdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/Drivers");

PreparedStatement statement = conn.prepareStatement("SELECT * fom employee");
ResultSet result = statement.executeQuery();
while(result.next()){
System.out.println(result.getString(1)+ " "+ result.getString(2));
}
}
}

这是我收到的错误。

运行:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysqljdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at JDBCInsertValues.main(JDBCInsertValues.java:14)

Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我的驱动程序位于项目库中。

最佳答案

驱动名称好像不对

尝试

Class.forName("com.mysql.jdbc.Driver").newInstance();

代替

Class.forName("com.mysqljdbc.Driver");

关于Java JDBC MySQL连接错误: ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23445096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com