我正在使用 NetBeans IDE 7.0.1,并且正在使用 Microsoft 的 JDBC 测试 Microsoft SQL Server 连接。我有这个测试程序:
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());
}
}
}
我将 NetBeans VM Options 属性设置为-Djava.library.path=C:\lib
.
当我在 IDE 中运行代码时,程序在 executeQuery 处冻结并“永远”运行。不返回错误并且不引发超时。
但是,如果我构建包然后运行它,java -Djava.library.path=C:\lib -jar TestSQL.jar
我会得到预期的数据返回:Count = 7349
.