19

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

}

gurpalrattu
  • 211
  • 1
  • 2
  • 8
  • 1
    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 Nov 08 '15 at 02:49
  • @xralf how quickly do you get the error? immediately or few seconds? – Salman A May 04 '23 at 18:41
  • @SalmanA few seconds – xralf May 04 '23 at 18:42
  • @xralf if you're sure that port 1433 is open then check if the instance name is correct. Usually it is `mssqlserver` (in which case you can remove it). If you specified instance name incorrectly in connection string you'll get some kind of error but it might be indistinguishable from a general port blocked/tcpip disabled/similar error. – Salman A May 04 '23 at 18:52

5 Answers5

56

Have you enabled 'Named Pipes' and 'TCP/IP'?

  1. Open the 'Sql Server Configuration Manager' 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.
Yauheni Leaniuk
  • 418
  • 1
  • 6
  • 15
JohnG79
  • 1,485
  • 1
  • 17
  • 23
3

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

1

I look at the ERRORLOG file for troubleshooting information. This file is usually located at:

C:\Program Files\Microsoft SQL Server\MSSQL<nn>.<instance name>\MSSQL\Log\ERRORLOG

The <nn> is the SQL Server version number and <instance name> is the instance name. The file contains useful information in the beginning:

2023-04-01 11:11:44.15 Server      Microsoft SQL Server 2012 (SP1) - 11.0.3156.0 (X64) 
    May  4 2015 18:48:09 
    Copyright (c) Microsoft Corporation
    Standard Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)

.
.
.
2023-04-01 11:11:44.16 Server      Server process ID is 4668.
.
.
.
2023-04-01 11:11:44.16 Server      Command Line Startup Parameters:
     -s "SQLSERVER_INSTANCE_2"
.
.
.
2023-04-01 11:11:45.12 spid4s      Server name is 'SQLMACHINE1\SQLSERVER_INSTANCE_2'. This is an informational message only. No user action is required.
.
.
.
2023-04-01 11:11:45.16 spid10s     Server is listening on [ 'any' <ipv6> 1433].
2023-04-01 11:11:45.16 spid10s     Server is listening on [ 'any' <ipv4> 1433].
2023-04-01 11:11:45.16 spid10s     Server local connection provider is ready to accept connection on [ \\.\pipe\SQLLocal\SQLSERVER_INSTANCE_2 ].
2023-04-01 11:11:45.16 spid10s     Server local connection provider is ready to accept connection on [ \\.\pipe\MSSQL$SQLSERVER_INSTANCE_2\sql\query ].
.
.
.

From the above you should be able to find out the following information about the SQL Server instance:

  • Start up date and time
  • Version
  • Process ID
  • Instance Name (MSSQLSERVER for the default instance)
  • IP and port number (if TCP/IP protocol is enabled, 1433 is the default port)
  • Named pipe path (even if named pipe protocol is disabled)

Once you have this information, build the connection URL:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

Default values are used for unspecified url components. Make sure to escape \ inside string literals.

Note that when using instance name without port number, a request is made to SQL Server browser service. That, too, needs to be running and UDP port 1434 must be open.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Thank you, I will try it tomorrow. It's interesting that SQLApp works (and RemoDB and my app doesn't work). I remember that in SQLApp the port wasn't specified. – xralf May 04 '23 at 19:25
  • 1
    Server name must be specified. Not specifying instance name -> default instance is assumed, not specifying port -> 1433 is assumed. – Salman A May 04 '23 at 19:40
  • I added instance name and it writes now [this](https://stackoverflow.com/questions/32766114/sql-server-jdbc-error-on-java-8-the-driver-could-not-establish-a-secure-connect) error message. I already added ";encrypt=true;trustServerCertificate=true;" but didn't help. – xralf May 05 '23 at 06:44
  • Unfortunately this could be or many reasons... outdated driver, outdated sql server, disabled ssl protocols and more. Try with `encrypt=false`. This will disable encryption thus no ssl issues. – Salman A May 05 '23 at 12:03
  • I had to use the jtds driver and with instance parameter it works. For microsoft driver I created a new bounty. Thanks a lot. – xralf May 05 '23 at 12:10
0

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

Rahul jha
  • 31
  • 5
0

My solution: Client: DBeaver Auth: Windows Authentication

After taking the steps:

  • Enable tcp/ip
  • Enabling named pipes

Connection string: localhost\SQLEXPRESS (that backslash made all the difference).

ouflak
  • 2,458
  • 10
  • 44
  • 49