com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed.-Collection of common programming errors


  • msdn I am trying to connect to SQL Server 2005 Express with JDBC.   I am getting the following exception:

    Code Snippet

    String connectionUrl = “jdbc:sqlserver://localhost:1433;” +            “databaseName=IFC3;”;        // Declare the JDBC objects.        Connection con = null;        Statement stmt = null;        ResultSet rs = null;                   try {                // Establish the connection.                Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);                    con = DriverManager.getConnection(connectionUrl,”ifc2″,”password”); com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)    at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)    at java.sql.DriverManager.getConnection(DriverManager.java:512)    at java.sql.DriverManager.getConnection(DriverManager.java:171)    at connectURL.main(connectURL.java:43)I am using the code example that came from Microsoft for making a JDBC connection.  I looked at my configuration of SQL Server and change to use a static port 1433.   I shut off Windows firewall to make sure it was not blocking communication. When I try to telnet to port 1433 it saysConnecting To 1433…Could not open connection to the host, on port 23: ConnectfailedAny suggestions?Thanks,Tom

  • 13 Answers


  • msdn1

    jdbc:sqlserver://localhost\SQLEXPRESS/Databases/EPS:1357;user=epsingall;password=welkom1; Slashes/backslashes like this! Then my error was gone.


  • msdn2

    Hi, I have done the below changes Microsoft SQL Server 2005 –> Configuration Tools –> SQL Server Configuration ManagerPart I (This will connect to your SQL EXPRESS)- Now, under it go to SQL Server 2005 Network Configuration–>Protocols for SQLEXPRESS.- Now, enable the VIA options by double click it.

    – Then, open TCP/IP part and set TCP Dynamic Ports: 1433 (or something else) under Protocal–>IP Address–>IP All

    Part II (This will connect to your MS SQL Server)- Now, under it go to SQL Server 2005 Network Configuration–>Protocols for MSSQLSERVER.- Now, enable the VIA options by double click it.- Then, open TCP/IP part and set TCP Dynamic Ports: 1433 (or something else) under Protocal–>IP Address–>IP AllBut still i am getting the exception:SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host JENESYS5/SQLEXPRESS, port 1433 has failed. Error: “null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host JENESYS5/SQLEXPRESS, port 1433 has failed. Error: “null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)        at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)        at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)        at java.sql.DriverManager.getConnection(DriverManager.java:582)        at java.sql.DriverManager.getConnection(DriverManager.java:207)        at main.main(main.java:20)BUILD SUCCESSFUL (total time: 16 seconds)Could you please help me to resolve this issue.Thanks and Regards,V.Vithya


  • msdn3

    hi,
       i am facing the problem

    The TCP/IP connection to the host localhost, port 1433 has failed. Error: “Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.so i tried to change my port number in configuration manager..but when i open it fails to open saying you do not premission or server is unreachable…..i thought there was a problem during installation…so i unistalled sql server and installed it again…but the error repeats…can any1 help me rectifying the problem..thanks,

    anusha


  • msdn4

    I get exactly the same error:

    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host ROLAND/SQLEXPRESS, port 1471 has failed. Error: “null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.

    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)

    ….Strange thing is, I can connect with OSQL.Even stanger, I have a JDBC jndi installed under my app server, than can connect on the port. That is using exaclty the same JDBC driver.

    But under Hibernate, it gives this error.


  • msdn5

    jdbc:sqlserver://localhost\SQLEXPRESS/Databases/EPS:1357;user=epsingall;password=welkom1; Slashes/backslashes like this! Then my error was gone.


  • msdn6

    You are completely right… I just tried with this URL and it worked for me too!!! THANKS!!


  • msdn7

    I was facing same problem. But after changing the configuration as mentioned, it is working for me. The connection URL when the server has a Instance is as follows:

      try {          Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);         String connectionUrl = “jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;” +             “databaseName=MB;user=sa;password=123;”;         System.out.println(“Successful”);         Connection con = DriverManager.getConnection(connectionUrl);         System.out.println(“Successful”);         } catch (SQLException e) {             System.out.println(“SQL Exception: “+ e.toString());         } catch (ClassNotFoundException cE) {             System.out.println(“Class Not Found Exception: “+ cE.toString());

            }

    Thanks & Regards,

    M. Rabbi


  • msdn8

    Sorry guys.  I follow this thread and I still have the same problem.

    I have SQL Server 2008

    Instance=TRISTAN-PC

    DatabaseName=ClientData

    Port as usual=1433

    User=sa

    password=123

    I also made sure that i could get access to the database from outside this computer on the network using this User and Password and that worked. But the code doesn’t work and tried many variant of your suggestions.  Any one have an idea?

    And here is my code with my last try?

            // Create a variable for the connection string.         String connectionUrl = “jdbc:sqlserver://localhost:1433;” +

                “databaseName=ClientData;”;

            // Declare the JDBC objects.

            Connection con = null;         Statement stmt = null;         ResultSet rs = null;         DataStructure dataIn;                 try {             // Establish the connection.

                Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

                //Global variable holding the connection             LoadVariables.conDBPath = DriverManager.getConnection(connectionUrl,”sa”,”123″);                         //data model container

                dataIn = SQLTypePartner.selectType*Partner();

                System.out.println(dataIn.toString());         }                 // Handle any errors that may have occurred.         catch (Exception e) {             e.printStackTrace();         }         finally {             dataIn = null;             if (rs != null) try { rs.close(); } catch(Exception e) {}             if (stmt != null) try { stmt.close(); } catch(Exception e) {}             if (con != null) try { con.close(); } catch(Exception e) {}         }//try


  • msdn9

    I found my problem, when I took a look at the config under SQL Native Client 10.0 Configuration, Client Protocols and TCP/IP, I saw Default port 1433 and I assume it was the real port no for the connection.  I look further and Saw under SQL Server Network Configuration, Protocol for SQLServer and TCP/IP, the port number there was completely different (52477)

    I try that one and it work without a glitch?

    Thanks Guy this thread helped


  • msdn10

    I found my problem, when I took a look at the config under SQL Native Client 10.0 Configuration, Client Protocols and TCP/IP, I saw Default port 1433 and I assume it was the real port no for the connection.  I look further and Saw under SQL Server Network Configuration, Protocol for SQLServer and TCP/IP, the port number there was completely different (52477)

    I try that one and it work without a glitch?

    Thanks Guy this thread helped

    You are right. This was helpful for me too!!!!!


  • msdn11

    Thank u for the gr8 help. It worked for me.


  • msdn12

    Hello Portaladmin,

    Just followed your steps…But this is not working.I’m trying to access SQL server 2005 table from my HWC iPhone application using Sybase Unwired Platform. Just posted a question here:

    Please help me.


  • msdn13

    Remember to restart the services under SQL Server Services. 

    I changed the port number to 1433 under IpAll and restarted the services as well as enabling TCP/IP and making that port number 1433 and it worked for me. best of luck.