10
votes

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

}

3
Have you done all of the things that are suggested by the error message? All of them? Have you actually verified that the server is >>listening<< on 127.0.0.1:1433?Stephen C

3 Answers

35
votes

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.
1
votes

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

0
votes

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