本文介绍了JDBC蚂蚁运行的build.xml的classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有蚂蚁从运行的麻烦。当我从类直线运行
文件内容如下它编译和运行 JASONS-的MacBook-PRO:SRC JS $ java的Hw5
学生ID:1
名字:杰森
姓氏
电话:555-220-5169
电子邮件:[email protected]
个人标语:永远不要说永不 当它运行正常如上code的下面一行用
连接连接=的DriverManager.getConnection(JDBC:德比:/用户/ JS /桌面/ JavaDevelopmentAnt / HW5_JDBC / lib目录/ Hw5Db); 我的code 导入的java.sql *。
进口的java.util.Enumeration; 公共类Hw5 {
公共静态无效的主要(字串[] args){
尝试{
连接连接=的DriverManager.getConnection(JDBC:德比:/用户/ JS /桌面/ JavaDevelopmentAnt / HW5_JDBC / lib目录/ Hw5Db);
//连接连接=的DriverManager.getConnection(JDBC:德比:Hw5Db); // Ant文件code
声明语句= connection.createStatement();
ResultSet中的resultSet = Statement.executeQuery的(SELECT * FROM STUDENT); 而(resultSet.next()){
INT studentID = resultSet.getInt(student_id数据);
字符串的firstName = resultSet.getString(名字);
字符串的lastName = resultSet.getString(姓氏);
字符串手机= resultSet.getString(PHONE);
字符串email = resultSet.getString(EMAIL);
字串口头禅= resultSet.getString(PERSONAL_TAGLINE);
的System.out.println(学号+ studentID +\\ n+
名字:+的firstName +\\ n+
姓:+的lastName +\\ n+
电话:+手机+\\ n+
电子邮件:+电子邮件+\\ n+
个人标语:+口头禅+\\ n);
}
resultSet.close();
将statement.close();
connection.close()时;
}
赶上(的SQLException SQLE){
通信System.err.println(SQL异常:+ SQLE);
}
}
} 但是当我从我的build.xml试试我改变code的下面一行
连接连接=的DriverManager.getConnection(JDBC:德比:Hw5Db); 因为我认为数据库的jar文件(Hw5Db)是我的build.xml的classpath 我的build.xml
< XML版本=1.0&GT?;
<项目名称=Hw5默认值=编译BASEDIR =&gt的帮助。
<物业环境=ENV/>
<属性名=SRC值=$ {} BASEDIR / src目录/>
<属性名=bin的价值=$ {BASEDIR} / bin中/>
<属性名=LIB值=$ {} BASEDIR / lib目录/>
<属性名=DOC值=$ {BASEDIR} /文件/>
<属性名=构建值=$ {BASEDIR} /打造/> <目标NAME =prepare描述=设置临时目录,以支持构建>
< MKDIR DIR =$ {}构建/>
< MKDIR DIR =$ {}斌/>
< /目标与GT; <目标名称=编译取决于=prepare描述=编译Java源代码>
< javac的SRCDIR =$ {src}字符串DESTDIR =$ {}建设包括=** / * java的listfiles =YESincludeantruntime =false的>
< / javac的>
< /目标与GT; <目标名称=部署依赖=编译>
<罐子destfile =$ {}斌/Hw5.jarBASEDIR =$ {}打造/>
<罐子destfile =$ {}斌/Hw5Db.jarBASEDIR =$ {} lib目录/>
< /目标与GT; <目标名称=跑取决于=部署描述=运行项目>
< java的叉=真正的类名=Hw5>
<类路径PATH =$ {}斌/Hw5.jar/>
<类路径PATH =$ {}斌/Hw5Db.jar/>
< / JAVA>
< /目标与GT; <目标名称=干净>
<删除DIR =$ {}构建/>
<删除DIR =$ {}斌/>
< /目标与GT;
< /项目>JASONS-的MacBook-PRO:HW5_JDBC jsteindorf $蚂蚁运行
构建文件:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build.xml prepare:
[MKDIR]创建目录:/用户/ JS /桌面/ JavaDevelopmentAnt / HW5_JDBC /编译
[MKDIR]创建目录:/用户/ JS /桌面/ JavaDevelopmentAnt / HW5_JDBC /箱 编译:
[javac的]源文件编译为/用户/ JS /桌面/ JavaDevelopmentAnt / HW5_JDBC /编译
[javac的] /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/src/Hw5.java 部署:
[罐子]大厦罐子:/Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5.jar
[罐子]大厦罐子:/Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5Db.jar 跑:
[考试] SQL异常:java.sql.SQLException中:找到了JDBC没有合适的驱动程序:德比:Hw5Db BUILD SUCCESSFUL
总时间:2秒 我想,我只是不能得到它的工作
解决方案
我固定的问题,build.xml文件并没有找到该数据库驱动程序
类路径PATH =$ {} env.DERBY_HOME /lib/derby.jar
类路径PATH =$ {} env.DERBY_HOME /lib/derbytools.jar
和与数据库的连接路径本来应该
JDBC:德比:./的lib / Hw5Db
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 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
这篇关于JDBC蚂蚁运行的build.xml的classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!