jdbc ant 运行 build.xml 类路径

发布于 2024-12-03 18:29:44 字数 4884 浏览 0 评论 0原文

    I'm having trouble running from ant.  When I run straight from the class 
    file as follows it compiles and runs

    Jasons-MacBook-Pro:src js$ java Hw5
    Student ID:  1
    First Name:  Jason
    Last Name:  S
    Phone:  555-220-5169
    Email:  [email protected]
    Personal Tagline:  Never say never

    The following line of code is used when it runs correctly as above
    Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");

    my code

    import java.sql.*;
    import java.util.Enumeration;

    public class Hw5{
       public static void main (String [] args){
          try{
             Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");
             //Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");//ant file code
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT");

             while(resultSet.next()){
                int studentID = resultSet.getInt("STUDENT_ID");
                String firstName = resultSet.getString("FIRSTNAME");
                String lastName = resultSet.getString("LASTNAME");
                String phone = resultSet.getString("PHONE");
                String email = resultSet.getString("EMAIL");
                String mantra = resultSet.getString("PERSONAL_TAGLINE");
                System.out.println("Student ID:  " + studentID + "\n" + 
                                "First Name:  " + firstName + "\n" + 
                                "Last Name:  " + lastName + "\n" +
                                "Phone:  " + phone + "\n" +
                                "Email:  " + email + "\n" +
                                "Personal Tagline:  " + mantra + "\n");
             }
             resultSet.close();
             statement.close();
             connection.close();         
          }
          catch(SQLException sqle){
             System.err.println("SQL Exception: " + sqle);
          }
       }
    }



    but when I try from my build.xml  i change the following line of code to
    Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");

    because i think database jar file (Hw5Db) is in the classpath of my build.xml

    my build.xml


<?xml version="1.0"?>
   <project name="Hw5" default="compile" basedir=".">
   <property environment="env"/>
   <property name="src" value="${basedir}/src"/>
   <property name="bin" value="${basedir}/bin"/>
   <property name="lib" value="${basedir}/lib"/>
   <property name="doc" value="${basedir}/doc"/>
   <property name="build" value="${basedir}/build"/>

   <target name="prepare" description="Setting up temporary directory to support build">
      <mkdir dir="${build}"/>
      <mkdir dir="${bin}"/>
   </target>

   <target name="compile" depends="prepare" description="compile java sources">
      <javac srcdir="${src}" destdir="${build}" includes="**/*.java" listfiles="yes" includeantruntime="false">
      </javac>
   </target>

   <target name="deploy" depends="compile">
      <jar destfile="${bin}/Hw5.jar" basedir="${build}"/>
      <jar destfile="${bin}/Hw5Db.jar" basedir="${lib}"/>
    </target>

    <target name="run" depends="deploy" description="run the project">
       <java fork="true" classname="Hw5">
          <classpath path="${bin}/Hw5.jar"/>
          <classpath path="${bin}/Hw5Db.jar"/>
       </java>
    </target>

    <target name="clean">
       <delete dir="${build}"/>
       <delete dir="${bin}"/>
    </target>
</project>

Jasons-MacBook-Pro:HW5_JDBC jsteindorf$ ant run
    Buildfile: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build.xml

    prepare:
        [mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
        [mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin

    compile:
        [javac] Compiling 1 source file to /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
        [javac] /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/src/Hw5.java

    deploy:
          [jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5.jar
          [jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5Db.jar

    run:
         [java] SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:derby:Hw5Db

    BUILD SUCCESSFUL
    Total time: 2 seconds    

    I'm trying, I just can't get it to work
    I'm having trouble running from ant.  When I run straight from the class 
    file as follows it compiles and runs

    Jasons-MacBook-Pro:src js$ java Hw5
    Student ID:  1
    First Name:  Jason
    Last Name:  S
    Phone:  555-220-5169
    Email:  [email protected]
    Personal Tagline:  Never say never

    The following line of code is used when it runs correctly as above
    Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");

    my code

    import java.sql.*;
    import java.util.Enumeration;

    public class Hw5{
       public static void main (String [] args){
          try{
             Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");
             //Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");//ant file code
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT");

             while(resultSet.next()){
                int studentID = resultSet.getInt("STUDENT_ID");
                String firstName = resultSet.getString("FIRSTNAME");
                String lastName = resultSet.getString("LASTNAME");
                String phone = resultSet.getString("PHONE");
                String email = resultSet.getString("EMAIL");
                String mantra = resultSet.getString("PERSONAL_TAGLINE");
                System.out.println("Student ID:  " + studentID + "\n" + 
                                "First Name:  " + firstName + "\n" + 
                                "Last Name:  " + lastName + "\n" +
                                "Phone:  " + phone + "\n" +
                                "Email:  " + email + "\n" +
                                "Personal Tagline:  " + mantra + "\n");
             }
             resultSet.close();
             statement.close();
             connection.close();         
          }
          catch(SQLException sqle){
             System.err.println("SQL Exception: " + sqle);
          }
       }
    }



    but when I try from my build.xml  i change the following line of code to
    Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");

    because i think database jar file (Hw5Db) is in the classpath of my build.xml

    my build.xml


<?xml version="1.0"?>
   <project name="Hw5" default="compile" basedir=".">
   <property environment="env"/>
   <property name="src" value="${basedir}/src"/>
   <property name="bin" value="${basedir}/bin"/>
   <property name="lib" value="${basedir}/lib"/>
   <property name="doc" value="${basedir}/doc"/>
   <property name="build" value="${basedir}/build"/>

   <target name="prepare" description="Setting up temporary directory to support build">
      <mkdir dir="${build}"/>
      <mkdir dir="${bin}"/>
   </target>

   <target name="compile" depends="prepare" description="compile java sources">
      <javac srcdir="${src}" destdir="${build}" includes="**/*.java" listfiles="yes" includeantruntime="false">
      </javac>
   </target>

   <target name="deploy" depends="compile">
      <jar destfile="${bin}/Hw5.jar" basedir="${build}"/>
      <jar destfile="${bin}/Hw5Db.jar" basedir="${lib}"/>
    </target>

    <target name="run" depends="deploy" description="run the project">
       <java fork="true" classname="Hw5">
          <classpath path="${bin}/Hw5.jar"/>
          <classpath path="${bin}/Hw5Db.jar"/>
       </java>
    </target>

    <target name="clean">
       <delete dir="${build}"/>
       <delete dir="${bin}"/>
    </target>
</project>

Jasons-MacBook-Pro:HW5_JDBC jsteindorf$ ant run
    Buildfile: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build.xml

    prepare:
        [mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
        [mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin

    compile:
        [javac] Compiling 1 source file to /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
        [javac] /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/src/Hw5.java

    deploy:
          [jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5.jar
          [jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5Db.jar

    run:
         [java] SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:derby:Hw5Db

    BUILD SUCCESSFUL
    Total time: 2 seconds    

    I'm trying, I just can't get it to work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡水深流 2024-12-10 18:29:44

不确定,但也许您需要在使用之前注册驱动程序。

Class.forName("class_of_driver").getInstance();  

之后:

Connection connection = DriverManager.getConnection("jdbc:derby:./Hw5Db");

Not sure but maybe you need register the driver before use it.

Class.forName("class_of_driver").getInstance();  

And after:

Connection connection = DriverManager.getConnection("jdbc:derby:./Hw5Db");
半步萧音过轻尘 2024-12-10 18:29:44

java 任务上重复的 classpath 对我来说看起来有点奇怪。请尝试以下方法:

<java fork="true" classname="Hw5">
     <classpath path="${bin}/Hw5.jar:${bin}/Hw5Db.jar"/>
</java>

The duplicate classpath on the java task looks a bit strange to me. Try the following instead:

<java fork="true" classname="Hw5">
     <classpath path="${bin}/Hw5.jar:${bin}/Hw5Db.jar"/>
</java>
离不开的别离 2024-12-10 18:29:44

我解决了这个问题,build.xml 没有找到数据库驱动程序

classpath path="${env.DERBY_HOME}/lib/derby.jar

classpath path="${env.DERBY_HOME}/lib/derbytools.jar

和数据库的连接路径应该是

jdbc:derby:./lib/Hw5Db

i fixed the issue, the build.xml wasn't finding to the database drivers

classpath path="${env.DERBY_HOME}/lib/derby.jar

classpath path="${env.DERBY_HOME}/lib/derbytools.jar

and the connection path to the database should have been

jdbc:derby:./lib/Hw5Db

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文