Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}

like image 798
gurpalrattu Avatar asked Nov 08 '15 02:11

gurpalrattu


People also ask

How do I fix the TCP IP connection to the host localhost port 1433 has failed?

If the Microsoft Windows registry TcpPort key is not using the default SQL Server port of 1433, test the JDBC connection string using the port set in the TcpPort registry key. If test connection still fails, disable any firewall software or ensure the firewall has a rule to allow communication on the port being used.

Could not open connection to host on port 1433 Connect failed?

Double click on TPC/IP which is on left panel. On Protocol tab if Enabled is No Then Make Yes. Then click on IP Address tab. Scroll down and last IPALL, set TCP Port as 1433.

Why is port 1433 blocked?

Port 1433 disabled on the MS-SQL Server In previous versions of MS-SQL server, port 1433 was enabled (open) by default, but as a hardening procedure implemented to improve security of the MS-SQL server, port 1433 is disabled in current versions of MS-SQL Server.


2 Answers

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.
like image 118
JohnG79 Avatar answered Sep 19 '22 09:09

JohnG79


This error usually come when SQL server not accepting TCP/IP Connection, pls try below steps it will work for sure.

1)open run and add command SQLServerManager15.msc 2)click on network configuration then "protocols for MSSQLSERVER" 3)Select protocol name - "TCP\IP" and make sure that it is enable if not then pls make it enable. 4)Check the property and find port in IP address tab.

Restart the server, it should work

like image 41
anshul dubey Avatar answered Sep 19 '22 09:09

anshul dubey