6

I have a local MS SQL Server and I am trying to connect to it using JTDS in java. Here is the connection string:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/stock","sa","password");

And the server properties:
name: USER-PC\SQLEXPRESS
root directory: c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL

I am getting a Exception in thread "main" java.sql.SQLException: Network error IOException: Connection refused: connect error.

How can I remedy this?

9
  • Have you looked here? connectionstrings.com May 9, 2012 at 18:41
  • Connectionstrings... Service Unavailable. HTTP Error 503. The service is unavailable. May 9, 2012 at 18:44
  • "Connection refused" typically means that either there's nothing listening on that port or a firewall is actively blocking your connection. Since you're connecting to localhost, I'd guess the former. Try connecting with a "native" SQL Server tool (i.e. management studio or sqlcmd) to boil it down to the simplest thing you can try.
    – Ben Thul
    May 9, 2012 at 19:26
  • @BenThul i can connect through management studio just fine
    – Mike
    May 9, 2012 at 19:31
  • @Mike: When you connect, are you using "localhost,1433" as the server name? Making sure we're comparing apples to apples.
    – Ben Thul
    May 9, 2012 at 19:33

3 Answers 3

12

Check the following:

  • You have enabled mixed mode security, i.e. so you can connect with username/password (rather than using Windows authentication)
  • The TCP/IP protocol is enabled. Open SQL Server Configuration Manager, then in SQL Server Network config select Protocols and Enable TCP/IP.
  • Try passing just one String to getConnection using this format:

    DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/stock;instance=SQLEXPRESS;user=sa;password=password")
    
  • If you are using this format (naming the instance) then the SQL Server Browser service needs to be running (in Services). If this is greyed out then the Service is probably disabled, so enable it and then start it.

9
  • with that change the error is now Exception in thread "main" java.sql.SQLException: Unable to get information from SQL Server: localhost.
    – Mike
    May 9, 2012 at 20:03
  • Have you definitely enabled TCP/IP? See jtds.sourceforge.net/faq.html#instanceGetInfo May 9, 2012 at 20:10
  • The TCP/IP is definitely enabled and the fire wall was pulled down and it still wont connect.
    – Mike
    May 9, 2012 at 20:31
  • Bit of a random thought- try using 127.0.01 instead of localhost May 9, 2012 at 20:39
  • Another thought- is the SQL Server Browser service running? It needs to be for TCP/IP connections May 9, 2012 at 20:48
0

Port 1433 is the port used by the default instance. You're using SQLEXPRESS, which is a named instance. Your connection string should include something like:

sqlserver://localhost\SQLEXPRESS
0

SQL Server Browser service is disable by default. If you're developing .Net app, you do not need to start SQLBrowser, but if you're using JTDS in java, you will need it started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.