none
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed.

    Question

  • 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 says


    Connecting To 1433...Could not open connection to the host, on port 23: Connect
    failed


    Any suggestions?
    Thanks,
    Tom
    Monday, May 21, 2007 3:21 PM

Answers

All replies

  • Hi,

    Please enable the 1433 port, if you are using MSSQL 2005 server. By port for TCP/IP is configured to take dynamically, so make it to 1433. Now, how follow the below step.

    Microsoft SQL Server 2005 --> Configuration Tools --> SQL Server Configuration Manager
    Part 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 All

    Smile

    Regards,
    PortalAdmin
    Monday, September 22, 2008 6:57 PM
  • Hi,


     I have done the below changes

    Microsoft SQL Server 2005 --> Configuration Tools --> SQL Server Configuration Manager
    Part 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 All

    But 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




    Monday, April 06, 2009 4:44 AM
  • 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

    Monday, April 20, 2009 12:21 AM
  • 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.

    Tuesday, June 02, 2009 4:12 PM
  • jdbc:sqlserver://localhost\SQLEXPRESS/Databases/EPS:1357;user=epsingall;password=welkom1; Slashes/backslashes like this! Then my error was gone.
    Tuesday, June 02, 2009 5:38 PM
  • You are completely right... I just tried with this URL and it worked for me too!!! THANKS!!
    Monday, August 24, 2009 6:40 PM
  • 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

    Sunday, December 12, 2010 7:35 AM
  • 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

    Thursday, December 23, 2010 2:50 AM
  • 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

    Thursday, December 23, 2010 2:23 PM
  • 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!!!!!
    Friday, May 20, 2011 2:44 PM
  • Thank u for the gr8 help. It worked for me.
    Wednesday, June 06, 2012 7:04 AM
  • 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:

    How to set connection affinity mask in SQL server 2005 when enabling VIA protocol.

    http://stackoverflow.com/questions/17837096/how-to-set-connection-affinity-mask-in-sql-server-2005-when-enabling-via-protoco/17850529#17850529

    Please help me.
    Thursday, July 25, 2013 7:15 AM
  • 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.

    Thursday, September 05, 2013 5:16 AM