0

我有一个在 MySQL 数据库上执行 CRUD 的 Java 程序。

为此,我一直在使用:

Eclipse Indigo
Java 1.6
MySql-Connector-java-5.1.19-bin.jar


我尝试在一台新机器 上执行相同的代码:

Eclipse Juno
Java 1.7
MySql-Connector-java-5.1.19-bin.jar. 

我已将 MySql 连接器添加到 Eclipse Juno 中项目的构建路径。

但是,ant 构建失败,并出现有关缺少应从连接器获取的类和包的错误:

Buildfile: /home/rudra/Softwares/openfire/openfire_src/build/build.xml
init:
compile:
[javac] Compiling 14 source files to /home/rudra/Softwares/openfire/openfire_src/work/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.5
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:3: error: package com.mysql.jdbc does not exist
[javac] import com.mysql.jdbc.Connection;
[javac]                      ^
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:4: error: package com.mysql.jdbc does not exist
[javac] import com.mysql.jdbc.PreparedStatement;
[javac]                      ^
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:5: error: package com.mysql.jdbc does not exist
[javac] import com.mysql.jdbc.ResultSet;
[javac]                      ^
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:21: error: cannot find symbol
[javac]             PreparedStatement stat = (PreparedStatement)connect.prepareStatement(query2);
[javac]             ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:21: error: cannot find symbol
[javac]             PreparedStatement stat = (PreparedStatement)connect.prepareStatement(query2);
[javac]                                       ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:21: error: cannot find symbol
[javac]             PreparedStatement stat = (PreparedStatement)connect.prepareStatement(query2);
[javac]                                                                ^
[javac]   symbol:   method prepareStatement(String)
[javac]   location: variable connect of type Connection
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:23: error: cannot find symbol
[javac]             ResultSet result = (ResultSet) stat.executeQuery();
[javac]             ^
[javac]   symbol:   class ResultSet
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:23: error: cannot find symbol
[javac]             ResultSet result = (ResultSet) stat.executeQuery();
[javac]                                 ^
[javac]   symbol:   class ResultSet
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:28: error: cannot find symbol
[javac]                 PreparedStatement preparedStmt = (PreparedStatement) connect.prepareStatement(query);
[javac]                 ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:28: error: cannot find symbol
[javac]                 PreparedStatement preparedStmt = (PreparedStatement) connect.prepareStatement(query);
[javac]                                                   ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:28: error: cannot find symbol
[javac]                 PreparedStatement preparedStmt = (PreparedStatement) connect.prepareStatement(query);
[javac]                                                                             ^
[javac]   symbol:   method prepareStatement(String)
[javac]   location: variable connect of type Connection
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:41: error: cannot find symbol
[javac]                 PreparedStatement statement = (PreparedStatement) connect.prepareStatement("Insert into user values (?,?,?);");
[javac]                 ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:41: error: cannot find symbol
[javac]                 PreparedStatement statement = (PreparedStatement) connect.prepareStatement("Insert into user values (?,?,?);");
[javac]                                                ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:41: error: cannot find symbol
[javac]                 PreparedStatement statement = (PreparedStatement) connect.prepareStatement("Insert into user values (?,?,?);");
[javac]                                                                          ^
[javac]   symbol:   method prepareStatement(String)
[javac]   location: variable connect of type Connection
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:73: error: cannot find symbol
[javac]             PreparedStatement statement = (PreparedStatement) connect.prepareStatement(query);
[javac]             ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:73: error: cannot find symbol
[javac]             PreparedStatement statement = (PreparedStatement) connect.prepareStatement(query);
[javac]                                            ^
[javac]   symbol:   class PreparedStatement
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:73: error: cannot find symbol
[javac]             PreparedStatement statement = (PreparedStatement) connect.prepareStatement(query);
[javac]                                                                      ^
[javac]   symbol:   method prepareStatement(String)
[javac]   location: variable connect of type Connection
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:75: error: cannot find symbol
[javac]             ResultSet result = (ResultSet) statement.executeQuery();
[javac]             ^
[javac]   symbol:   class ResultSet
[javac]   location: class UserDB
[javac] /home/rudra/Softwares/openfire/openfire_src/src/java/org/jivesoftware/openfire/UserDB.java:75: error: cannot find symbol
[javac]             ResultSet result = (ResultSet) statement.executeQuery();
[javac]                                 ^
[javac]   symbol:   class ResultSet
[javac]   location: class UserDB

谁能帮我这个?

4

1 回答 1

0

这个问题中得到启示:

假设您的 build.xml 中有一个编译目标,看起来类似于:

<!-- Compile application -->
  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes" includeantruntime="false">
      <classpath refid="classpath" />
    </javac>
  </target>

成功编译所需要做的就是定义类路径,使其包含项目 lib 目录中的所有 jar,如下所示:

  <path id="classpath">
    <fileset dir="${lib.dir}" includes="*.jar"/>
  </path>

我从您的问题中了解到,您在 Eclipse 项目中添加了对连接器 jar 的引用。您可以在将连接器 jar 复制到 Eclipse 项目的 lib 文件夹中尝试上述建议。

或者,虽然我认为这不是一个很好的建议,但您可以将项目导出为 Ant Build 并检查由此生成的 build.xml。这可以通过转到 File -> Export,输入“ant”进行过滤,然后选择“Ant Buildfiles”来完成,如下所示:

导出为 Ant BuildFiles

于 2012-08-04T13:01:14.340 回答