gpt4 book ai didi

java.lang.ClassNotFoundException : com. mysql.jdbc.Driver 运行jar文件时

转载 作者:行者123 更新时间:2023-11-29 05:54:37 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

大家好,
这是我的示例代码:

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "admin";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}

我在 eclipse 中运行它没问题,但是我构建了 jar 文件并在命令行 java -jar Test.jar 上运行我得到了这个错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)

请帮我解决一下。

谢谢!!!

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