I am trying to connect to a MS SQL Server database running on the same machine as the application but I keep getting the error:
No suitable driver found for jdbc:sqlserver://localhost:1433;database=SQLTEST;user=TEST;password=1234567890;encrypt=true;trustServerCertificate=false;loginTimeout=30;
I am running MS SQL Server 2017 and JDK1.8.x.
The first thing I did was creating a user in the following way in SSMS using T-SQL:
USE SQLTEST
CREATE ROLE TEST
GRANT SELECT, INSERT ON SCHEMA :: [dbo] TO TEST
CREATE LOGIN TEST_LOGIN WITH PASSWORD = '1234567890'
CREATE USER USER_TEST FROM LOGIN TEST_LOGIN
ALTER ROLE TEST ADD MEMBER USER_TEST
I then used Maven to add the driver to my project in the pom.xml file:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.2.jre8</version>
</dependency>
Followed by adding and altering the example code given by Microsoft (note that this should become a JAX-RS REST-API, that is why the annotations are there):
@Path("t2")
public class T2 {
@GET
public String show() {
String r = "";
String connectionUrl =
"jdbc:sqlserver://localhost:1433;"
+ "database=SQLTEST;"
+ "user=TEST;"
+ "password=1234567890;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";
try (Connection connection = DriverManager.getConnection(connectionUrl);) {
// Code here.
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
r = e.getMessage();
}
return r;
}
}
Running this results in the error as given in the start of this post. Did I forget something or did I do something wrong?