找不到com.mysql.jdbc.Driver的类不是类路径问题 [英] class not found exception for com.mysql.jdbc.Driver not a classpath problem

查看:164
本文介绍了找不到com.mysql.jdbc.Driver的类不是类路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了好几个星期,现在我只是在圈子里奔跑.我收到运行时错误:在获取连接com.mysql.jdbc.Driver时[class]中未找到类异常".

I've been working on this for weeks, and now I'm just running in circles. I'm getting the runtime error: " class not found exception in [class] while getting connection com.mysql.jdbc.Driver ".

连接功能如下:

/**
 * @return a MySQL connection to the server as specified above 
 */
private static Connection getConnection() {
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(url, username, password);
  } catch (ClassNotFoundException e) {
    System.err.println("class not found exception in [class] while getting connection " + e.getMessage());
  } catch (SQLException e) {
    System.err.println("sql exception in [class] while getting connection " + e.getMessage());
  }
  return con;
} //end getConnection

我已经将$CLASSPATH设置了一百次,但仍然没有运气.这是现在的样子:

I have set the $CLASSPATH a hundred times a hundred ways, and still no luck. This is what it looks like now:

# echo $CLASSPATH
:/usr/share/java/mysql-connector-java-5.1.13-bin.jar:/usr/share/java/mysql-connector-java-3.0.17-ga-bin.jar:

这两个文件都存在,并且在正确的位置包含了Driver.class文件.

Both of those files exist and have the Driver.class file in them in the right place.

我什至在导出jar之前尝试将类导入到我的代码中,但这引起了很多其他问题.

I even tried importing the class into my code before exporting the jar, but that caused a world of other problems.

我正在使用Eclipse 3.5.2,并且服务器正在运行Fedora 13 x86_64

I'm using Eclipse 3.5.2 and the server is running Fedora 13 x86_64

请帮助.

我正在使用以下命令在服务器上远程运行代码:

edit: I'm running the code remotely on my server with the command:

# java -jar program.jar

推荐答案

在Eclipse中有一种简单的方法可以解决此问题:

There is an easy way to solve this problem in Eclipse:

  1. 右键单击您的项目
  2. 选择属性"
  3. 选择部署程序集"
  4. 点击添加->"Java构建路径条目"
  5. 选择包含数据库连接器的用户库(例如mysql-connector-java-5.1.20.jar).

这篇关于找不到com.mysql.jdbc.Driver的类不是类路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆