gpt4 book ai didi

java - 连接到 SQL Server 时出现 SQL 异常

转载 作者:行者123 更新时间:2023-12-04 20:43:05 25 4
gpt4 key购买 nike

我知道这是一个重复的问题。我发现了与他们非常相似的问题和解决方案,但我仍然对此感到震惊。

我正在使用 eclipse 将我的 java 应用程序与 Microsoft sql server 2008 数据库连接起来。以下是我的代码

    import java.sql.*;
public class ConnectionTest2 {
public static void main(String [] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Sample;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl,"","");
System.out.println("Connected");
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}

我已经通过进入 sql server 配置管理器并在 IPALL 下将端口号设置为 1433 来启用 tcp/ip 和 VIA。

我尝试了很多方法,但我无法找到解决以下错误的方法

SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

我也禁用了 windows 防火墙,但无法连接。

请帮帮我。

最佳答案

我遇到了这个问题,我通过为服务器实例启用 TCP/IP 协议(protocol)来修复它。你可以通过

  1. 打开 SQL Server 配置管理器
  2. 展开 SQL Server 网络配置
  3. 为 MSSQLSERVER 选择协议(protocol)
  4. 启用 TCP/IP

此后需要重启SQL Server服务才能生效。

关于java - 连接到 SQL Server 时出现 SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10035291/

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