gpt4 book ai didi

java - JDBC 驱动程序问题

转载 作者:行者123 更新时间:2023-11-30 04:53:24 25 4
gpt4 key购买 nike

我正在尝试让 JDBC 在我的 Windows 7 上工作。我将其添加到 CLASSPATH (“G:/workspace/mysql-connector-java-5.1.18-bin.jar”)。重新启动,因为我认为这可能是问题所在。但 Class.forName("com.mysql.jdbc.Driver").newInstance() 仍然无法正常工作。它抛出以下异常:

Exception in thread "main" 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)
at JDBCTest.main(JDBCTest.java:18)

这是我类(class)中的代码:

....  
private static Connection connect=null;
private static Statement statement=null;
private PreparedStatement preparedstament=null;
private static ResultSet resultset=null;

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

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

connect=DriverManager.getConnection("jdbc:mysql://localhost/feedback?user=root&password=root");
statement=connect.createStatement();

我该如何解决这个问题?

最佳答案

我不相信 Eclipse 在启动 Java 应用程序时会使用 CLASSPATH 环境变量。您需要将其添加到项目的构建路径中,或者显式配置启动配置以包含它。将其添加到构建路径可能更简单:

  • 右键单击项目
  • 选择构建路径
  • 选择“添加外部文件”
  • 浏览到 jar 文件

关于java - JDBC 驱动程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352375/

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