This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes SQLServerException - could not connect to the host on port 1433 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQLServerException - could not connect to the host on port 1433" Watch "SQLServerException - could not connect to the host on port 1433" New topic
Author

SQLServerException - could not connect to the host on port 1433

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi guys,

I'm getting a 'could not connect to the host on port 1433' error with the below code. I've checked my URL string several times with what MS says it should be, and my Windows Firewall is off. I've ensured that MSSQLServer is using TCP 1433 and running, BUT, and I'm not sure why this is, when I do netstat, I do not see anything listening on 1433. The SQL Server process in the task manager does not match with anything from the netstat output, and the .exe (I used -anob switches with netstat) does not match up. The closest thing I see is SQL Browser listening on port 1434. Am I missing some important detail?

Full error:
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, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at database.Main.main(Main.java:23)




Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Patrick,

Instead of "localhost" , try with IP "127.0.0.1". Or make sure if you have "127.0.0.1 localhost" entry in hosts file.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Balu Sadhasivam wrote:Patrick,

Instead of "localhost" , try with IP "127.0.0.1". Or make sure if you have "127.0.0.1 localhost" entry in hosts file.


Hi Balu,

Yes, originally I had 127.0.0.1, but changed to localhost just for troubleshooting. My hosts file does contain this entry, also. Does anything else stick out to you? It seems that my connection string is OK (not sure of this, though), and that my SQL Server service may not be listening?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You won't see SQL Server listening on 1433 via netstat, I think it reports the port as machinename:ms-sql-s or machinename:ms-sql-m. Do you have anti virus software running? Does it block connections?

You are trying to use integrated authentication, if you swap to SQL Server authentication can you connect?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Paul Sturrock wrote:You won't see SQL Server listening on 1433 via netstat, I think it reports the port as machinename:ms-sql-s or machinename:ms-sql-m. Do you have anti virus software running? Does it block connections?

You are trying to use integrated authentication, if you swap to SQL Server authentication can you connect?



I didn't see AVG blocking, but I did not disable it; guess I should. I haven't tried with SQL Server authentication, because I'm having problems with it. When I create a new account which uses SQL Server auth, I can't log in as the account I just created. Strange.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Patrick,

Check out if this thread could help you
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Patrick Brooks wrote:

I didn't see AVG blocking, but I did not disable it; guess I should. I haven't tried with SQL Server authentication, because I'm having problems with it. When I create a new account which uses SQL Server auth, I can't log in as the account I just created. Strange.


Have you enabled your server to accept both kinds authentication? I think out the box its Windows integrated only.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Balu Sadhasivam wrote:
Patrick,

Check out if this thread could help you


Balu,

Yes, I have checked that thread out, and it didn't resolve the problem. This is a strange one, unless there is something wrong with my connection string and I don't see it.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Paul Sturrock wrote:
Have you enabled your server to accept both kinds authentication? I think out the box its Windows integrated only.


Aha, I'll read up on that when I get home. I didn't spend too much time on it yesterday, becuase I was still able to log on with Windows Integrated Auth.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Nope, still didn't work. I turned my AVG off, and used SQL Server authentication, which I allowed this evening. I still get the 'connection refused' error. When I do a System.out.println(url) it comes out as jdbc:sqlserver://localhost:1433;databaseName=MYBASE;user=test2;password=Test1234 . Any ideas?

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Patrick,
Did you ping the port ?
Check of you cud trouble shoot the sql server logs as mentioned here
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hmm, interesting. I had to create a database app for a job at work, and with the exact same settings and connection string, I was able to get into the localhost database, which is what I was trying to do on this laptop. No biggie, as it was more a test from this laptop than anything else, but it would be nice to know what the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQLServerException - could not connect to the host on port 1433