130

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));
}
2
  • 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 Commented Sep 17, 2013 at 5:17
  • 2
    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. Commented Oct 19, 2022 at 5:46

9 Answers 9

287
  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.
Sign up to request clarification or add additional context in comments.

7 Comments

And don't forget to restart your SQL instance after making the changes.
Restart your SQL instance as following: at the "SQL Server Configuration Manager" -> "SQL Server 2012 Services" right-click on "SQL Server" and choose "Restart".
Pay attention to Make sure the TCP Port is 1433 under IPAII.
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 (nucleustechnologies.com/blog/…)
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
|
143

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

1 Comment

After following these steps, I had to stop and restart my server for it to work. Thanks!
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

2 Comments

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
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
17

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.

1 Comment

helped me a lot. Browser was disabled which is key component which must be running to make client connections.
8

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

2 Comments

In my case , the tcp/ip status is disabled. I changed it and set the port 1433.
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.
2

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

3 Comments

I think OP is using sql server not mysql
@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.
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
-1

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

Comments

-1

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.

Comments

-1

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.

1 Comment

Duplicate of this answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.