113

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.

Error:

The TCP/IP connection to the host 127.0.0.1, port 1433 has failed. Error: "Connection refused: connect. 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.".

My code:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
 //"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" +       "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and 
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set

while (rs.next())
{
    System.out.println(rs.getInt(1));
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Aysha Nijhawan
  • 1,171
  • 2
  • 8
  • 4
  • The local computer has refused to allow you to connect to the port 1433. This is likely a firewall issue. Also make sure that there is something actually listening on the port – MadProgrammer Sep 17 '13 at 05:17
  • 1
    This is a notorious bug. From local windows machine, i am able to connect to the database via application as well as SSMS. But on linux server, this issue is coming. Further, i tried telnet on my linux vm, telnet is also fine. not sure what is the problem. – Manish Bansal Oct 19 '22 at 05:46

9 Answers9

261
  1. Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
  2. Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
  3. On the Protocol tab, notice the value of the Listen All item.
  4. Click the IP Addresses tab: If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll. If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
  5. Make sure the TCP Port is 1433.
  6. Click OK.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
prince
  • 2,636
  • 1
  • 11
  • 3
  • 18
    And don't forget to restart your SQL instance after making the changes. – JimGoods Oct 09 '15 at 10:30
  • 17
    Restart your SQL instance as following: at the "SQL Server Configuration Manager" -> "SQL Server 2012 Services" right-click on "SQL Server" and choose "Restart". – Developer Marius Žilėnas Nov 10 '15 at 07:36
  • 12
    Pay attention to Make sure the TCP Port is **1433** under IPAII. – Developer Marius Žilėnas Sep 27 '16 at 05:54
  • Or change the port number in your application to the value of the TCP Dynamic Ports under IPAII. That is what I did to get this working, because I can't change the value. – AlieneilA May 04 '18 at 08:35
  • Also be sure that sql server authentication is enabled: Go to the Properties of selected server (from the right click menu). Now go to the Security page and under Server authentication choose the option SQL Server and Window Authentication mode. Now expand Security > Logins and right-click the server name, and select Properties. ... Click OK and restart the SQL server (https://www.nucleustechnologies.com/blog/error-sql-server-error-starting-esri_sde-service-93-dbms-error-code-18456) – Patrick Koorevaar Dec 23 '20 at 16:02
  • 1
    the SQL instance failed to start when i followed your instructions, but when i remove the port 1433 and restart, it starts successfully. any advice – Smith Jan 15 '22 at 08:12
  • Clean TCP dynamic port (set null) even if it's zero (0) – Nayeem Bin Ahsan Jan 12 '23 at 13:35
134

Easy Solution

Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box

Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.

enter image description here

Md. Naushad Alam
  • 8,011
  • 6
  • 25
  • 23
22

The error is self explanatory:

  • Check if your SQL server is actually up and running
  • Check SQL server hostname, username and password is correct
  • Check there's no firewall rule blocking TCP connection to port 1433
  • Check the host is actually reachable

A good check I often use is to use telnet, eg on a windows command prompt run:

telnet 127.0.0.1 1433

If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem

gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • 3
    I tried telnet 127.0.01 1433 , it says Connecting to 127.0.01`could not open connection to the host or port 1433 connection failed – Aysha Nijhawan Sep 17 '13 at 06:34
  • 2
    Hence it's very likely you haven't installed the server, the server isn't running, or you have a firewall preventing the connection. This normally isn't a java program issue – gerrytan Sep 17 '13 at 06:39
15

Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager. enter image description here

If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start. In some cases above state can stop though TCP connection to port 1433 is assigned.

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
  • helped me a lot. Browser was disabled which is key component which must be running to make client connections. – Mayur Sep 10 '20 at 13:23
6

Open %windir%\System32 folder and find SQLServerManagerXX.msc

For example:

C:\Windows\System32\SQLServerManager14.msc

Go to protocols settings then enable TCP/IP port is 1433 by default

enter image description here

enter image description here

Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
  • In my case , the tcp/ip status is disabled. I changed it and set the port 1433. – Kevin Onofre Jan 01 '22 at 21:27
  • Strange, I could connect to my local SQL Server via Azure Data Studio and SSMS, but not via DBeaver or AWS Schema Conversion Tool until I enabled this. – JohnnyFun Apr 29 '23 at 15:19
3

As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.

Here are the commands that can be useful to troubleshoot:

  1. Use netstat -a to check whether sql server is listening on the desired port
  2. As gerrytan mentioned in answer, you can try to do the telnet on the host and port
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I think OP is using sql server not mysql – SpringLearner Sep 17 '13 at 05:18
  • @AyshaNijhawan Have u checked the port it is listening to? Also try to connect to the sql server through command prompt or a sql client using the same ip/port and credentials. – Juned Ahsan Sep 17 '13 at 06:16
  • Port is 1433, and i have tried using by putting it in connection string. Named pipes, memory, and TCP/IP are enables in SQL server N/W config -> Protocols for MSSQLSERVER – Aysha Nijhawan Sep 17 '13 at 06:23
0

important:
after any changes or new settings you must restart SQLSERVER service. run services.msc on Windows

Clemsang
  • 5,053
  • 3
  • 23
  • 41
Mahdi
  • 57
  • 6
0

If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.

Toshihiko
  • 325
  • 1
  • 8
  • 20
0

Open Firewall GUI,Open TCP 1433 rule,go to scope tab and add your IP address under Remote IP Address and the problem is solved.

Talley Ouro
  • 186
  • 1
  • 4