gpt4 book ai didi

java - JDBC连接失败,错误: TCP/IP connection to host failed

转载 作者:IT老高 更新时间:2023-10-28 11:49:53 27 4
gpt4 key购买 nike

我想将 Java 类文件与 SQL Server 2012 连接。我已使用 SQL Server 身份验证登录,但连接时收到错误消息。

错误:

The TCP/IP connection to the host 127.0.0.1, 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 andaccepting TCP/IP connections at the port. Make sure that TCPconnections to the port are not blocked by a firewall.".

我的代码:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set

while (rs.next())
{
System.out.println(rs.getInt(1));
}

最佳答案

  1. 打开 SQL Server 配置管理器,然后展开 SQL Server 2012 网络配置。
  2. 单击 InstanceName 的协议(protocol),然后确保在右侧面板中启用 TCP/IP,然后双击 TCP/IP。
  3. 在 Protocol 选项卡上,注意 Listen All 项的值。
  4. 单击 IP 地址选项卡:如果 Listen All 的值为 yes,则此 SQL Server 2012 实例的 TCP/IP 端口号是 IPAll 下 TCP Dynamic Ports 项的值。如果 Listen All 的值为 no,则此 SQL Server 2012 实例的 TCP/IP 端口号是特定 IP 地址的 TCP 动态端口项的值。
  5. 确保 TCP 端口为 1433。
  6. 点击确定。

关于java - JDBC连接失败,错误: TCP/IP connection to host failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18841744/

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