Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC connection failed, error: TCP/IP connection to host failed

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)); } 
like image 417
Aysha Nijhawan Avatar asked Sep 17 '13 05:09

Aysha Nijhawan


People also ask

How do I fix the TCP IP connection to the host localhost port 1433 has failed?

If the Microsoft Windows registry TcpPort key is not using the default SQL Server port of 1433, test the JDBC connection string using the port set in the TcpPort registry key. If test connection still fails, disable any firewall software or ensure the firewall has a rule to allow communication on the port being used.

How do I enable TCP IP connection in SQL Configuration Manager?

On the Start menu, click All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager. Click SQL Server 2012 Services. Expand the SQL Server 2012 Network Configuration node, and then select Protocols for MSSQLServer (SQL Instance Name) . Right-click TCP/IP, and then click Enable.

What is JDBC connection error?

JDBC connection errors. Connection errors are typically caused by incorrect or invalid connection string parameters, such as an invalid host name or database name. Verify that you are using correct and valid connection string parameters for the Gate.


2 Answers

  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.
like image 99
prince Avatar answered Oct 08 '22 05:10

prince


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

like image 44
Md. Naushad Alam Avatar answered Oct 08 '22 03:10

Md. Naushad Alam