2
votes

I know that this is a repeated question. I have found very similar questions and solutions to them but still i'm struck with it.

I'm using eclipse to connect my java application with microsoft sql server 2008 database. Following is my code

    import java.sql.*;
public class ConnectionTest2 {
  public static void main(String [] args) {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Sample;integratedSecurity=true";
        Connection con = DriverManager.getConnection(connectionUrl,"","");
        System.out.println("Connected");
        } catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString());
        } 
catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }
  }
}

I've enabled the tcp/ip and VIA by going in to sql server configuration manager and set the port number to 1433 under IPALL.

I've tried in many ways but i'm unable to find a solution to the following error

SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, 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.".

I've also disabled the windows firewall but failed to connect.

Please help me out.

2
Do you have any anti-virus or VPN software that may also have a firewall on it?John Koerner
This might be too basic, but are you sure the port is right and available? Try the command telnet localhost 1433 in cmd to make sureUdi Cohen

2 Answers

3
votes

It is what it says: connection is refused. Have you tried 'telnet 1433'?

4
votes

I had this problem and I fixed it by enabling the TCP/IP protocol for the server instance. You can do that by

  1. Open SQL Server Configuration Manager
  2. Expand SQL Server Network Configuration
  3. Select Protocols for MSSQLSERVER
  4. Enable TCP/IP

After this, You need to restart the SQL Server service to make it effective.