通过ant build.xml文件

时间:2015-07-21 19:17:50

标签: java xml eclipse sql-server-2008 ant

  • 我有Java文件连接到SQL服务器数据库并从中检索数据。
  • 当我从eclipse执行这个java文件时,它成功连接到数据库并从数据库中检索数据。
  • 但是当我从ant build.xml文件中调用相同的java文件时,它会在创建连接对象时抛出SQL服务器异常。

无法得到可能存在的问题。请帮忙!!

抛出Sql异常: -

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 10.6.1.67, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
     [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
     [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
     [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
     [java]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     [java]     at java.lang.reflect.Method.invoke(Unknown Source)
     [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
     [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
     [java]     at org.apache.tools.ant.Target.execute(Target.java:435)
     [java]     at org.apache.tools.ant.Target.performTasks(Target.java:456)
     [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
     [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
     [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
     [java]     at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:36)
     [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:452)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:139)

以下是我的java文件代码: -

    package util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;

    public class JavaSQLConnection{ 
        public static void main(String[] args) throws Exception {
            String connectionUrl="jdbc:sqlserver://10.6.1.67";
            String port="1433";
            String databaseName="Microsoft";
            String user="selenium";
            String password="password123";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection conn = DriverManager.getConnection(connectionUrl+":"+port+";databaseName="+databaseName+";user="+user+";password="+password);

            String Sql = "select name, address, url from School where schoolid=?";
            PreparedStatement stmt = conn.prepareStatement(Sql);
            ResultSet rs = null;
            stmt.setInt(1, 875632);
            rs = stmt.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("url"));
            }
            stmt.close();
            conn.close();
        }
    }

这就是我创建ant build.xml的方法:(我执行ant target 运行

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="Sample" basedir=".">

    <property environment="env" />
    <property name="proj.home" value="${basedir}" />
    <property name="lib.location" value="${proj.home}/lib" />
    <property name="build.location" value="${proj.home}/build" />
    <property name="classes.location" value="${build.location}\compiledclasses" />
    <property name="src.location" value="${proj.home}/src" />


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


    <target name="clean">
        <delete failonerror="false" dir="${build.location}" />
    </target>   


    <!-- compile -->
    <target name="compile" depends="clean">
        <mkdir dir="${build.location}" />
        <mkdir dir="${classes.location}" />
        <javac includeantruntime="true" srcdir="${src.location}" destdir="${classes.location}" classpathref="classpath" />
    </target>


    <!-- run -->
    <target name="run" depends="compile">
        <java classname="util.JavaSQLConnection" classpath="${classes.location}" classpathref="classpath" />
    </target>

</project>

0 个答案:

没有答案