2

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.

Arion
  • 31,011
  • 10
  • 70
  • 88
user1074122
  • 373
  • 3
  • 6
  • 14
  • Do you have any anti-virus or VPN software that may also have a firewall on it? – John Koerner Apr 05 '12 at 20:12
  • 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 sure – Udi Cohen Apr 05 '12 at 20:14
  • possible duplicate of [How do I connect to a SQL Server 2008 database in Java with JDBC?](http://stackoverflow.com/questions/2451892/how-do-i-connect-to-a-sql-server-2008-database-in-java-with-jdbc) – Ken White Apr 05 '12 at 20:15

2 Answers2

4

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.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Nikki
  • 41
  • 1
3

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

ahanin
  • 892
  • 4
  • 8
  • _telnet localhost 1433_ says _Could not open connection to the host, on port 1433: Connect failed_...where am i going wrong – user1074122 Apr 05 '12 at 22:05
  • Your going wrong with trying to connect to a port on which server is not listening. If the door is closed, do you still hit it with your head? – ahanin Apr 06 '12 at 08:33