1

I am exploring ways to connect to a SQL database using JDBC in Java and interact with it. Problem is no matter how I follow the syntax to make such connection it does not let me to connect to that instance of SQL Server on my laptop. I have seen other people being able to run such instances on their localhost instead and be able to connect to it via JDBS but I could not find any walkthroughs as how I can do the same. Any help with it will be greatly appreciated. Here is the connection I have on my laptop:

enter image description here

And here the JDBC connection URL that I try to make based on this on my code:

package helpers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbHandler {

    private static final String connectionUrl = "jdbc:sqlserver://LAPTOP-KAQ3H6IG//SQLEXPRESS:1433;database=pub;user=MyUserName;password=MyPassword";

    public static void addNewJobWithName(String jobName) {

        try (Connection connect = DriverManager.getConnection(connectionUrl)) {
            connect.createStatement().execute("INSERT INTO [pub].[dbo].[jobs] (job_id, job_desc, min_lvl, max_lvl) VALUES (2,'QA3', 50, 100);");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

And the error I receive:

The TCP/IP connection to the host LAPTOP-KAQ3H6IG//SQLEXPRESS, port 1433 has failed. Error: "LAPTOP-KAQ3H6IG//SQLEXPRESS. 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.". at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:237)

Thanks in advance for your help

Dale K
  • 25,246
  • 15
  • 42
  • 71
Mo Pishdar
  • 113
  • 1
  • 13
  • 1
    did you try `jdbc:sqlserver://localhost:1433;database=pub;user=M_Pishdar;password=750706`? – HariHaravelan Mar 15 '22 at 07:06
  • 1
    Does this answer your question? [The TCP/IP connection to the host localhost, port 1433 has failed error, need assistance](https://stackoverflow.com/questions/33590030/the-tcp-ip-connection-to-the-host-localhost-port-1433-has-failed-error-need-as) – SOS Mar 15 '22 at 07:18
  • @HariHaravelan ... yes, and it gives me the same error while I can access in SQL server studio management – Mo Pishdar Mar 15 '22 at 10:26
  • @SOS... Thanks, it helped solving a major part of the problem. I had to change the jdbc string as well by adding the suggested configurations from another post and it solved my problem, – Mo Pishdar Mar 15 '22 at 11:10
  • @HariHaravelan at the end of the day you were right... it must have been changed to localhost... only other trick was to add those additional checks to the string I mentioned in the answer below...thank you – Mo Pishdar Mar 15 '22 at 11:26
  • 1
    Makes no sense to use both the instance name `SQLEXPRESS` and a port `1433`. The instance name is used if it is a named instance with a dynamic port so it will be able to find which port it's on. Annoyingly, it seems jdbc does not support Shared Memory. If it did, you could use `.` as the machine name rather than `localhost` and it would use Shared Memory which is faster than TCP – Charlieface Mar 15 '22 at 14:04

1 Answers1

1

Did a combination of things based on the comments made here and some other posts I read on stack overflow regarding other things to do to fully solve this problem here are the things that I did:

  1. Changed my connection URL string to this:

"jdbc:sqlserver://localhost:1433;database=pub;user=MyUserName;password=MyPassword;encrypt=true;trustServerCertificate=true";

  1. On SQL Server configuration manager clicked on 'SQL Server Network Configuration' and enabled 'Named Pipes' and 'TCP/IP' in 'Protocols for SQLEXPRESS' (where my local MSSQL instance is setup).
  2. Clicking on TCP/IP in the same window, switched to the IP Address tab and entered '1433' as the port number for the IPAII section right at the end of the list (Leave the other port sections empty).
  3. Restarted SQL Server (SQLEXPRESS) from the list of services in Windows. Hope it can help other people having the same problem as well.
Mo Pishdar
  • 113
  • 1
  • 13