socket-locket
socket-locket

Reputation: 1

JDBC not connecting to local database even though I tried three different connection URLs

Let's provide some context:

  1. I have a local SQL server which goes by the name (local)/MSSQLLocalDB or the name of my device which is:"DESKTOP-T7CN5JN\LOCALDB#6173A439" .
  2. I am using a Java project with maven to manage dependencies.
  3. Using Java ORACLE JDK21
  4. I have established a connection in IntelliJ with the database (can write queries on the DB) and it provided the third URL in the provided snippet.
  5. The database uses windows authentication

Problem: As shown in the following code snippet I tried 3 different connection strings and all lead to runtime errors.

Goal: figure out what is the correct connection format to establish a connection and why none of these is working I feel like I tried looking everywhere for a solution

String connectionUrl1 = "jdbc:sqlserver://localhost:1433;databaseName =laptop_registry;integratedSecurity = true;encrypt=false";

String connectionUrl2 = "jdbc:sqlserver://DESKTOP-T7CN5JN\\LOCALDB#6173A439;databaseName = laptop_registry;integratedSecurity=true;encrypt=false";

String connectionUrl3 = "jdbc:jtds:sqlserver://./laptop_registry";

line 15: try (Connection conn = DriverManager.getConnection(<connectionUrlGoesHere>)
 ){...}catch.....

URL 1 results in the following error

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database "laptop_registry" requested by the login. The login failed. ClientConnectionId:f933922b-5a12-44f0-b100-3a6390845190
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:270)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:329)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:137)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:42)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$1LogonProcessor.complete(SQLServerConnection.java:6577)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:6889)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:5434)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:5366)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7745)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:4391)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:3828)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:3385)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:3194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1971)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1263)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at org.mainn.dbconnector.MSSQLDatabaseConnector.main(MSSQLDatabaseConnector.java:15)

URL 2 results in the following

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host DESKTOP-T7CN5JN, named instance localdb#6173a439 failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:7918)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:3680)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:3364)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:3194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1971)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1263)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at org.mainn.dbconnector.MSSQLDatabaseConnector.main(MSSQLDatabaseConnector.java:15)

URL 3 results in the following error

java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://./laptop_registry
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at org.mainn.dbconnector.MSSQLDatabaseConnector.main(MSSQLDatabaseConnector.java:15)

I searched quite a bit all I could find was guides that made me turn on/off TCP/IP in SQL Server Configuration Manager which lead to no avail.

I also extracted the jdbc_mssql.dll file into the Java 21 JDK so it shouldn't be a driver problem as stated by error 3.

Upvotes: 0

Views: 60

Answers (0)

Related Questions