我有一个连接到SQL Server的代码,该代码段在我的笔记本电脑中完美运行。但是,当我从另一台机器运行相同的代码时,会抛出异常:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host <DB>, port 1433 has failed.
Error: Permission denied: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
代码:
public static void main(String ... ar) throws SQLException {
String connectionUrl = "jdbc:sqlserver://<db>;databaseName=webdatabase;user=<dbuser>;password=<dbpass>";
Connection conn = DriverManager.getConnection(connectionUrl);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("<DB Query>");
System.out.println(rs);
System.out.println("adsasdasd");
}
答案 0 :(得分:1)
因此,问题是在运行代码时设置了一个系统属性。
问题是在代码中引入这一行:System.setProperty(&#34; java.net.preferIPv4Stack&#34;,&#34; true&#34;);
或者在VM选项中传递它: -Djava.net.preferIPv4Stack =真
答案 1 :(得分:0)
如果您已将MS SQL服务器配置为侦听TCP / IP流量,则可能是以下任何原因:
如果您已验证问题不属于上述类别,那么您可以尝试执行此操作以启用TCP / IP流量的端口1433
!
1433
。希望这有帮助!