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
0
A
回答
0
我固定的問題,這個build.xml沒有尋找到數據庫驅動程序
的classpath路徑=「$ {} env.DERBY_HOME /lib/derby.jar
的classpath路徑=「$ {} env.DERBY_HOME /lib/derbytools.jar
,並與數據庫的連接路徑應該是
的jdbc:德比:./的lib/Hw5Db
1
不知道,但也許你需要註冊前驅動程序中使用它。
Class.forName("class_of_driver").getInstance();
而經過:
Connection connection = DriverManager.getConnection("jdbc:derby:./Hw5Db");
1
在java
任務重複的classpath
看起來有點怪我。請嘗試以下操作來代替:
<java fork="true" classname="Hw5">
<classpath path="${bin}/Hw5.jar:${bin}/Hw5Db.jar"/>
</java>
相關問題
- 1. 在螞蟻的build.xml
- 2. 的build.xml螞蟻問題
- 3. 包括螞蟻的build.xml
- 4. 螞蟻不承認的classpath
- 5. 螞蟻運行
- 6. 重置螞蟻的build.xml爲運行Android的測試項目
- 7. 進口的build.xml螞蟻在Eclipse中
- 8. 螞蟻的build.xml清單文件讀取
- 9. NoClassDefFoundError的運行與螞蟻
- 10. 螞蟻android更改路徑build.xml
- 11. 從build.xml動態添加jar螞蟻
- 12. 什麼JVM運行螞蟻?
- 13. 用螞蟻運行java類
- 14. 運行JUnit任務螞蟻
- 15. 螞蟻 - 如何納入classpath中
- 16. 如何指定底層目錄,然後我們運行像螞蟻螞蟻-f somedir/DIR/build.xml文件
- 17. 使用螞蟻和常春藤的可執行jar - CLASSPATH問題
- 18. 構建步驟:「調用螞蟻」與「從shell運行螞蟻」
- 19. 如何運行螞蟻腳本,螞蟻沒有安裝?
- 20. 的NoClassDefFoundError在與螞蟻運行
- 21. 如何運行pylint的從螞蟻
- 22. 從螞蟻運行Automator生成的.app?
- 23. 的IntelliJ IDEA:螞蟻的build.xml - 屬性不在這裏
- 24. 指定了目錄中的build.xml螞蟻在Windows
- 25. 螞蟻的contrib需要1.8.2 build.xml文件,但會導致錯誤
- 26. 動態傳遞多個值在螞蟻的build.xml
- 27. 獲取螞蟻運行java文件
- 28. NoClassDefFoundError當編譯和運行與螞蟻
- 29. 螞蟻運行命令與管道
- 30. 螞蟻:後來命令不運行
對於JavaDB(作爲JDK的一部分捆綁在一起),這不是必需的。 – BalusC
嗯,謝謝@BalusC –