0

I know there are tons of similar questions that have been answered, but I've searched pretty much all of them and can't figure out the solution to my issue.

I am trying to connect to my SQL database with the below code, but I am getting an error. Before I show my code, I am able to connect to the same database using DBeaver with the below connection details:

enter image description here

I am trying to connect to the database by doing:

Connection connection = DriverManager
                    .getConnection("jdbc:mysql://localhost:1433/master", "sa", "MY_PASSWORD_HERE");

However, at this point, my code doesn't throw any error, but it throws the below error after about 2 minutes, which I assume it has basically given up and timed out.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

I have no idea what I'm doing wrong as I've followed the same procedures as the countless tutorials online. Any help is appreciated.

For reference, I am building my project with Maven. Here is a snippet of the pom.xml

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.19</version>
</dependency>

EDIT: running SELECT @@VERSION returns:

Microsoft SQL Server 2019 (RTM-CU4) (KB4548597) - 15.0.4033.1 (X64) 
    Mar 14 2020 16:10:35 
    Copyright (C) 2019 Microsoft Corporation
    Developer Edition (64-bit) on Linux (Ubuntu 18.04.4 LTS) <X64>

1 row(s) returned

Executed in 1 ms
The Impaler
  • 45,731
  • 9
  • 39
  • 76
Adam
  • 2,384
  • 7
  • 29
  • 66
  • 2
    Are you trying to connect to a **MySQL** engine, or to a **SQL Server** engine? Your screen shot looks like SQL Server, but your java code looks like MySQL. – The Impaler Apr 18 '20 at 15:27
  • @TheImpaler I'm not sure - could you tell me how I can find out? – Adam Apr 18 '20 at 15:29
  • 1
    Those are two very different animals. If you connected using DBeaver try running the statement: `SELECT @@VERSION` and tell us the result. Also, try `SELECT VERSION();` and tell us. – The Impaler Apr 18 '20 at 15:30
  • I have edited my question when running `SELECT @@VERSION`. Your other suggestion throws an error `Error: 'VERSION' is not a recognized built-in function name.` – Adam Apr 18 '20 at 15:33
  • 1
    OK, it's SQL Server 2019. I updated the tags in your question. Now, your Java code and Maven pom.xml are trying to connect to MySQL. You'll need to change all that. – The Impaler Apr 18 '20 at 15:35

1 Answers1

2

The problem is that your database is a SQL Server database, but you are using a JDBC driver and connection string suitable for MySQL. That's not going to work.

You'll need to change the dependency in your pom.xml. Use SQL Server's JDBC driver. For example:

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>7.4.1.jre8</version>
</dependency>

Then, in your Java code, you should change the JDBC URL. It should look like:

Connection connection = DriverManager
  .getConnection("jdbc:sqlserver://localhost:1433", "sa", "MY_PASSWORD_HERE");
The Impaler
  • 45,731
  • 9
  • 39
  • 76