SQL Exception while connecting to SQL server

Go To StackoverFlow.com

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.

2012-04-05 20:10
by user1074122
Do you have any anti-virus or VPN software that may also have a firewall on it - John Koerner 2012-04-05 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 sur - Udi Cohen 2012-04-05 20:14

possible duplicate of How do I connect to a SQL Server 2008 database in Java with JDBC?Ken White 2012-04-05 20:15


2

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

2012-04-05 20:13
by ahanin
telnet localhost 1433 says Could not open connection to the host, on port 1433: Connect failed...where am i going wron - user1074122 2012-04-05 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 2012-04-06 08:33


3

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.

2012-08-03 22:26
by Nikki
Tried that. Does not work - sweet dreams 2012-08-05 21:51
This helped me. It should be the real answer - Gallen 2014-02-04 19:49
Ads