locked
JDBC with SQL Express RRS feed

  • Question

  • The java code :

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

       String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
         "instance=SQLEXPRESS;databaseName=UPM;integratedSecurity=true;";
      Connection con = DriverManager.getConnection(connectionUrl);

    returns "connection refused" no matter what I do.

     

    I've used the surface config tool to enable TCP, I've added sqlsrvr.exe to the firewall exceptions, I've tried a dozen or so variations on the connection string.

    I can connect to the database using the Management Studio.

     

    Any ideas what I'm missing ????

    Thanks.

    Thursday, October 12, 2006 7:50 PM

Answers


  • This may be because by default MS SQL Server Express is configured to use dynamic TCP/IP ports, like named instances.

    Go into the SQL SERVER CONFIGURATION MANAGER
    open SQL SERVER 2005 Network Configuration
    open Protocols for SQLEXPRESS
    open TCP/IP properties
    on the IP ADDRESSES tab, check at the bottom if "TCP Dynamic Ports" has a value.

    if so, clear the value and leave that field blank.  Then change "TCP Port" to 1433 or whatever port you decide.
    Friday, October 27, 2006 6:11 PM
  •  

    As you are using a trusted connection can your verify that you are using the correct user in both systems.

    Friday, October 13, 2006 12:22 AM

All replies

  •  

    As you are using a trusted connection can your verify that you are using the correct user in both systems.

    Friday, October 13, 2006 12:22 AM
  • I'm running into the same problem.

    No problem connecting through VWD or SQL Management Studio...

    The code:

    String url =
          "jdbc:sqlserver://localhost;" +
          //"databaseName=TV;" +
          "portNumber=1433;" +
          "instanceName=./SQLEXPRESS;" +
          "loginTimeout=0;" +
          //"userName=Pierre;" +
          //"serverName=PG" +
          "integratedSecurity=true;";

    con = DriverManager.getConnection( url );

    Same bad result with all commented parameters.

    The message:

    The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect.

     

    Friday, October 13, 2006 9:00 PM

  • This may be because by default MS SQL Server Express is configured to use dynamic TCP/IP ports, like named instances.

    Go into the SQL SERVER CONFIGURATION MANAGER
    open SQL SERVER 2005 Network Configuration
    open Protocols for SQLEXPRESS
    open TCP/IP properties
    on the IP ADDRESSES tab, check at the bottom if "TCP Dynamic Ports" has a value.

    if so, clear the value and leave that field blank.  Then change "TCP Port" to 1433 or whatever port you decide.
    Friday, October 27, 2006 6:11 PM
  • These things helped me get the connection to work:

    1. Enable the SQL Server Browser service

    2. Use the SQL Server Configuration manager to enable all client protocols (named pipes, TCP/IP, shared memory)

    3. Set the priority order for protocols so that TCP/IP is first

    4. Create a SQL Server user that has the necessary permissions to the database you want to connect to

    5. Install the SQL Server JDBC driver from sourceforge instead of the one from Microsoft

    6. Use jdbc:jtds:sqlserver://localhost:1433;instance=SQLEXPRESS with the user name and password.  Do not use integratedsecurity

    • Proposed as answer by Mike MMC Wednesday, January 15, 2020 12:10 AM
    Monday, December 22, 2014 7:40 PM
  • This worked for a 2014 MS SQLEXPRESS instance.

    After I cleared out the dynamic port # and hardcoded a TCP Port, it worked.


    Friday, May 1, 2015 5:02 PM
  • that style of "instance=SQLEXPRESS" I think was huge for me.

    I think it didn't understand and format "localhost\sqlexpress" correctly at all -  looked at network packet capture and it was querying for "localhostsqlexpress.com:1433".

    Great save with this one!

    Wednesday, January 15, 2020 12:10 AM