gpt4 book ai didi

java - mysql JDBC 连接 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 06:32:29 24 4
gpt4 key购买 nike

我正在尝试在 Windows 2008 服务器上测试 mySQL JDBC 连接。我已经下载了 JDBC 驱动程序,它在“C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar”中创建了一个 jar 文件。当我运行一个小程序来测试我的 jdbc 连接时,出现“NoClassDefFound”错误。我在这里缺少什么?

我确实在类路径中设置了 jdbc jar

C:\>echo %CLASSPATH%
C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

我已将 jar 放在与我的 DBDemo.java (C:\test) 相同的位置

DemoDB.java

import java.sql.*;
import java.util.Properties;
public class DBDemo
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private static final String CONNECTION =
"jdbc:mysql://127.0.0.1/emotherearth";
public static void main(String[] args) throws
ClassNotFoundException,SQLException
{
System.out.println(dbClassName);
// Class.forName(xxx) loads the jdbc classes and
// creates a drivermanager class factory
Class.forName(dbClassName);
// Properties for user and password. Here the user and password are both 'paulr'
Properties p = new Properties();
p.put("user","paulr");
p.put("password","paulr");
// Now try to connect
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println("It works !");
c.close();
}
}

*错误*

C:\Program Files\Java\jdk1.6.0_45\bin>java c:\test\DBDemo
Exception in thread "main" java.lang.NoClassDefFoundError: c:\test\DBDemo
Caused by: java.lang.ClassNotFoundException: c:\test\DBDemo
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: c:\test\DBDemo. Program will exit.

最佳答案

C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

您的CLASSPATH 似乎没有.,那是DBDemo 所在的当前工作目录。因此,将 . 添加到您的 CLASSPATH 中。之后你的 CLASSPATH 会是这样的

.;C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

编辑

也尝试将 mysql-connector-java-5.1.34-bin.jar 的内容提取到一个文件夹中,并根据该文件夹更改 CLASSPATH

关于java - mysql JDBC 连接 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073624/

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