23

I keep on getting this error. I tried one solution from Stack Overflow (below), which did not work for me: SQL Exception while connecting to SQL server

I tried allowing port 1433 in the Windows firewall by using a link, that did not work either. Refer to section "To open a port in the Windows firewall for TCP access" http://msdn.microsoft.com/en-us/library/ms175043.aspx

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.

Please help me to fix this problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sweet dreams
  • 2,094
  • 13
  • 32
  • 46
  • What are your connection settings? Are you trying to connect to the default instance or a named instance? – Neil Aug 05 '12 at 23:48
  • @Neil - I just read about "default" and "named" instance. I typed a command and got a name. Is that the name for my instance ? select @@servername go The name is JUSTIN\SQLEXPRESS. – sweet dreams Aug 06 '12 at 00:08
  • 1
    Does your code look like `con = DriverManager.getConnection("jdbc:sqlserver://justin\\sqlexpress", "user", "pass");` ? – Neil Aug 07 '12 at 00:01
  • Returning after a long time. Don't even remember if i fixed this issue. – sweet dreams Mar 28 '15 at 17:09
  • @sweet dreams-- how did you managed to solve this issue? – vikrant rana Jul 31 '19 at 00:48
  • For me, adding port 1433 in firewall (as described in question) solved my issue. Thank you! – gordon613 May 14 '20 at 15:32

4 Answers4

56

You need to go to Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager

When it opens, go to SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress.

There you'll find the Protocol TCP/IP, if disabled then Enable it. Click on TCP/IP, you'll find its properties.

In these properties, remove all the TCP Dynamic Ports and add value of 1433 to all TCP ports, and restart your SQL Server Services > SQL Server

And it's done.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Parth Soni
  • 11,158
  • 4
  • 32
  • 54
18

Steps

  • Open the Sql Server Configuration Manager (Start -> Programs -> Microsoft SQL Server 2008 -> Configuration Tools)

  • Expand SQL Server Network Configuration -> [Your Server Instance]

  • Double click TCP/IP

  • Under Protocol:

  • Ensure Enabled is Yes

  • Under IP Addresses:

  • Scroll to the bottom and set the TCP Port under IPAll, (1433 by default)

  • Find the IP address you want to connect to and set Enabled and Active to Yes

RKannan
  • 251
  • 3
  • 3
3

To query database SQL Server with JDBC

Firstly, You need to find out the IP of your SQL Server and Enable TCP/IP and set TCP/IP port either. To do that, do these steps:

  • Open SQL Server Configuration Manager

    • Choose SQL Server Network Configuration -> Protocols for -> Right click on TCP/IP -> select Enable (if already enabled then move to next step) -> Right click to TCP/IP again -> Properties -> Choose tab IP Address -> In IPAll group: Clear value of TCP Dynamic Ports and set 1433 to TCP Port -> Click OK

      Note: Copy one of the IPs in the Properties table to do config later

    • Double click to SQL Server Services - > Right click to SQL Server (USER) ** -> Choose **Restart

Now, you already have the IP, config server as below:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://put.the.ip.here:1433;databaseName=<DB-Name>" />
        <property name="username" value="sa" />
        <property name="password" value="p@ssW0rd" />
</bean>

Note: Remove destroy-method="close" out of the bean

dqthe
  • 683
  • 5
  • 8
0

be sure tha in connectionUrl, your slash is oriented to left like this:

String connectionUrl = "jdbc:sqlserver://localhost**\**itis;databaseName=dbMediaStore;user=;password=";