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

javasql server

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

}

Best Answer

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.