java TCP/IP 连接到主机 localhost,端口 1433 已失败错误,需要帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33590030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:53:22  来源:igfitidea点击:

The TCP/IP connection to the host localhost, port 1433 has failed error, need assistance

javasql-server

提问by gurpalrattu

Full error I'm getting:

我得到的完整错误:

The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. 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.".

到主机 localhost 端口 1433 的 TCP/IP 连接失败。错误:“连接超时。验证连接属性。确保 SQL Server 实例正在主机上运行并在端口接受 TCP/IP 连接。确保到端口的 TCP 连接未被防火墙阻止。 ”。

I have already checked that TCP/IP is enabled, using port 1433, and TCP dynamic ports is empty. I have disabled windows firewall.

我已经检查过是否启用了 TCP/IP,使用端口 1433,并且 TCP 动态端口为空。我已经禁用了 Windows 防火墙。

Here is my code:

这是我的代码:

import java.sql.*;

public class DBConnect {




public static void main(String[] args) {
    // TODO Auto-generated method stub

    String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB1;instance=SQLSERVER;encrypt=true;TrustServerCertificate=true;";
    String user = "sa";
    String pass = "";
    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");


    Connection myConn = DriverManager.getConnection(dbURL, user, pass);
    try {
    Statement myStmt = myConn.createStatement();

    try {
    ResultSet myRs = myStmt.executeQuery("Select * from Login");



        while (myRs.next())
        {
            System.out.println(myRs.getString("Username"));
            System.out.println(myRs.getString("Password"));
        }
    }
    catch (Exception e)
    {
        System.out.println("Error with query");
    }
    }

    catch (Exception e)
    {
        System.out.println("Error connecting to database");
    }
    }

    catch (Exception e)
    {
        System.out.println(e);
    }
}

}

}

回答by JohnG79

Have you enabled 'Named Pipes' and 'TCP/IP'?

您是否启用了“命名管道”和“TCP/IP”?

  1. Open the 'Sql Server Configuration' application.

  2. In the left pane, go to 'SQL Server Network Configuration' -> 'Protocols for [instance-name]'

  3. Right-click on both 'Named Pipes' and 'TCP/IP' and select 'enable'.

  1. 打开“Sql Server 配置”应用程序。

  2. 在左窗格中,转到“SQL Server 网络配置”->“[实例名称] 的协议”

  3. 右键单击“命名管道”和“TCP/IP”并选择“启用”。

Have you used the correct port?

您是否使用了正确的端口?

  1. Double-click on 'TCP/IP'

  2. Select 'IP Addresses' tab

  3. Scroll to IPAII. Your port number is here.

  1. 双击“TCP/IP”

  2. 选择“IP 地址”选项卡

  3. 滚动到 IPAII。您的端口号在这里。



  1. Restart the 'SQL Server ([instance-name])' windows service.
  1. 重新启动“SQL Server ([instance-name])”Windows 服务。

回答by Rahul jha

And also make sure that on the same page TCP/IP is enabled

并确保在同一页面上启用了 TCP/IP