简体   繁体   English

executeQuery从不在NetBeans IDE中返回,但在由Java运行时可以工作

[英]executeQuery never returns in NetBeans IDE but works when run by java

I am using NetBeans IDE 7.0.1 and was just testing a Microsoft SQL Server connection using JDBC from Microsoft. 我正在使用NetBeans IDE 7.0.1,并且刚刚使用Microsoft的JDBC测试Microsoft SQL Server连接。 I have this test program: 我有这个测试程序:

package testsql;
import java.sql.*;
public class TestSQL {

    public static void main(String[] args) {
        Statement stmt = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionURL = 
                    "jdbc:sqlserver://foo.bar.com:1433;" +
                    "databaseName=flintstone;integratedSecurity=true;";
            con = DriverManager.getConnection(connectionURL);
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exception: " + e.toString());
        }

        try {
            String SQL = con.nativeSQL("SELECT COUNT(*) AS Count FROM fred");
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            while (rs.next()) {
                System.out.println("Count = " + rs.getString("Count"));
            }
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        }

    }
}

I have the NetBeans VM Options property set to -Djava.library.path=C:\\lib . 我将NetBeans VM Options属性设置为-Djava.library.path=C:\\lib

When I run the code in the IDE the program freezes at executeQuery and runs 'forever'. 当我在IDE中运行代码时,程序会冻结在executeQuery上并“永远”运行。 No error is returned and a time-out is not raised. 没有错误返回,并且不会引起超时。

However if I build the package and then run it with java -Djava.library.path=C:\\lib -jar TestSQL.jar I get the expected data returned: Count = 7349 . 但是,如果我生成该程序包,然后使用java -Djava.library.path=C:\\lib -jar TestSQL.jar运行它, java -Djava.library.path=C:\\lib -jar TestSQL.jar得到返回的预期数据: Count = 7349

I have the project working in the IDE now. 我现在在IDE中工作该项目。 It turned out to be the version of the JDK I was using. 原来是我正在使用的JDK版本。 Replacing JDK1.6 with JDK1.7 fixed the problem, it works as expected now. 用JDK1.7替换JDK1.6可以解决此问题,现在可以按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM