3
votes

I am using

  • Windows 7
  • Netbeans IDE 7.1.2
  • SQL Server Management Studio Express 2005
  • JDK1.6

I am getting the below error while connecting to the database:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

My connection string is:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
Connection connection = DriverManager.getConnection(  
    "jdbc:sqlserver://127.0.0.1:1433;databaseName=dbcm;" +  
    "user=sa;password=sa");

I've checked below things:

  1. In SQL Server Configuration Manager, Protocols for SQLEXPRESS : TCP/IP Enabled In fact I made enable remaining 3 too.
  2. SQL Server(SQLExpress) and SQL Browser Service both are running.
1
Did you check windows' firewall settings ?Pierre Henry
Try to omit the port number. Make sure SQLServerBrowser Service is running. Try using localhost instead of IP address. Check your firewall settings.MicSim
You must add your instance name like ;instanceName=SQLEXPRESS if you are using one.MicSim
@Pierre Henry : Yes,I checked firewall settings.bsm
@MicSim : SQLBrowser Service is running. Typed localhost instead of IP address also added instanceName. But still I am getting the same error.bsm

1 Answers

18
votes

I followed the steps below to successfully connect to an SQLExpress instance using JDBC:

  1. Configure TCP/IP communication with SQL Express
    1. Open SQL Server Configuration Manager.
    2. Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
    3. Set the status of TCP/IP protocol to "Enabled" (if it is already not).
    4. Open Properties window for TCP/IP, go to IP Addresses section.
    5. Go to the bottom of this property page and set the TCP Port under IPAll to 1433.
  2. Connect to the SQLExpress instance using Microsoft's JDBC driver for SQL Server
    1. JDBC URL: jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>