gpt4 book ai didi

java - 创建与 SQL Server 的 JDBC 连接时权限被拒绝

转载 作者:行者123 更新时间:2023-12-01 09:31:34 25 4
gpt4 key购买 nike

我有一个连接到 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");
}

最佳答案

因此,问题出在运行代码时设置系统属性。

问题是在代码中引入这一行:System.setProperty("java.net.preferIPv4Stack", "true");

或者将其传递到 VM 选项中:-Djava.net.preferIPv4Stack=true

关于java - 创建与 SQL Server 的 JDBC 连接时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com