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

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

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.".

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

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

}

回答1:

Have you enabled 'Named Pipes' and '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'.

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. Restart the 'SQL Server ([instance-name])' windows service.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!