I have a Spring Boot app which creates a database connection. It’s fairly standard code:
SQLServerConnection sqlServerConnection = (SQLServerConnection) DriverManager.getConnection(url);
I checked and the URL is correct and contains the right connection string.
It used to work fine, but after some configuration updates (changed the gradle version and some other seemingly unrelated things) the code above throws the exception that the driver can’t be found.
After some experimenting the fix turned out to be adding this line before trying to establish a connection:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Some background – the app is spring boot based. We generate a fat jar and I checked it – it contains the microsoft driver jar:
$ jar tf app.jar | grep jdbc
BOOT-INF/lib/mssql-jdbc-12.4.1.jre11.jar
My question is – what’s happening? From my understanding the Class.forName() is not required since 1.6.