Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to local MS SQL Server

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?

like image 641
Mike Avatar asked May 09 '12 18:05

Mike


People also ask

Can't connect to local mssql?

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." Open the SQL Server Configuration Manager. Expand SQL Server Network Configuration for the server instance in question.


1 Answers

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.

like image 125
Martin Wilson Avatar answered Oct 05 '22 08:10

Martin Wilson