Java JDBC 连接失败,错误:到主机的 TCP/IP 连接失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18841744/
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-08-12 11:33:36  来源:igfitidea点击:

JDBC connection failed, error: TCP/IP connection to host failed

javasql-serverjdbc

提问by Aysha Nijhawan

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication. But i am receiving error in connectivity.

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

ERROR: 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 and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

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

My code ---

我的代码---

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));
}

回答by Juned Ahsan

As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.

正如错误所说,您需要确保您的 sql server 正在运行并侦听端口 1433。如果服务器正在运行,那么您需要检查是否有一些防火墙规则拒绝端口 1433 上的连接。

Here are the commands that can be useful to troubleshoot:

以下是可用于故障排除的命令:

  1. Use netstat -ato check whether sql server is listening on the desired port
  2. As gerrytan mentioned in answer, you can try to do the telneton the host and port
  1. 使用netstat -a检查的SQL Server是否正在监听所需的端口上
  2. 正如回答中提到的gerrytan,您可以尝试telnet在主机和端口上执行

回答by gerrytan

The error is self explanatory:

错误是不言自明的:

  • Check if your SQL server is actually up and running
  • Check SQL server hostname, username and password is correct
  • Check there's no firewall rule blocking TCP connection to port 1433
  • Check the host is actually reachable
  • 检查您的 SQL 服务器是否真正启动并运行
  • 检查 SQL 服务器主机名、用户名和密码是否正确
  • 检查没有防火墙规则阻止 TCP 连接到端口 1433
  • 检查主机是否实际可达

A good check I often use is to use telnet, eg on a windows command prompt run:

我经常使用的一个很好的检查是使用 telnet,例如在 Windows 命令提示符下运行:

telnet 127.0.0.1 1433

If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem

如果出现黑屏,则表明网络连接已成功建立,这不是网络问题。如果您收到“无法打开与主机的连接”,则这是网络问题

回答by prince

  1. Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
  2. Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
  3. On the Protocol tab, notice the value of the Listen All item.
  4. Click the IP Addresses tab: If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll. If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
  5. Make sure the TCP Port is 1433.
  6. Click OK.
  1. 打开 SQL Server 配置管理器,然后展开 SQL Server 2012 网络配置。
  2. 单击 InstanceName 的协议,然后确保在右侧面板中启用了 TCP/IP,然后双击 TCP/IP。
  3. 在 Protocol 选项卡上,注意 Listen All 项的值。
  4. 单击“IP 地址”选项卡:如果“全部侦听”的值为“是”,则此 SQL Server 2012 实例的 TCP/IP 端口号就是 IPAll 下的“TCP 动态端口”项的值。如果 Listen All 的值为 no,则此 SQL Server 2012 实例的 TCP/IP 端口号是特定 IP 地址的 TCP 动态端口项的值。
  5. 确保 TCP 端口为 1433。
  6. 单击确定。

If there are any more questions, please let me know.

如果还有其他问题,请告诉我。

Thanks.

谢谢。

回答by Md. Naushad Alam

Easy Solution

简单的解决方案

Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box

开始->所有程序->Microsoft SQL Server 2012->配置工具->点击SQL Server配置管理器->展开SQL Server网络配置->协议->启用TCP/IP右键框

Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.

双击 TCP/IP 并转到 IP 地址点击并将端口 1433 置于 TCP 端口下。

enter image description here

在此处输入图片说明

回答by Abdur Rahman

Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager. enter image description here

转到开始->所有程序-> Microsoft SQL Server 2012-> 配置工具-> 单击 SQL Server 配置管理器。 在此处输入图片说明

If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start. In some cases above state can stop though TCP connection to port 1433 is assigned.

如果您看到 SQL Server/SQL Server 浏览器状态为“已停止”。右键单击 SQL Server/SQL Server 浏览器并单击开始。 在某些情况下,尽管分配了到端口 1433 的 TCP 连接,但上述状态可能会停止。

回答by Mahdi

important:
after any changes or new settings you must restart SQLSERVERservice. run services.mscon Windows

重要:
任何更改或新设置后,您必须重新启动SQLSERVER服务。运行services.mscWindows

回答by Toshihiko

If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.

如果您使用的是命名实例,则您使用的端口可能是 1434,而不是 1433,因此也请使用上述 telnet 或 netstat 进行检查。