17

Hello can you explain it to me, why is it instead of using com.mysql.jdbc.Driver I got an error

Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Your help is much appreciated

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
maria
  • 363
  • 2
  • 5
  • 14

2 Answers2

39

It isn't an error; it is a warning (or advisory) message resulting from a

Class.forName("com.mysql.jdbc.Driver")

call. Your code continues to run despite the message.

It is mainly telling you that the name of the driver class has changed to com.mysql.cj.jdbc.Driver. So, instead use:

Class.forName("com.mysql.cj.jdbc.Driver")

It is also letting you know that since Java 6 (JDBC 4.0) it is usually not necessary to manually load the driver class using Class.forName anyway, because JDBC is now able to load the correct driver itself (provided that the driver .jar is available on the class path).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • 1
    Could you please say why was `com.mysql.jdbc.driver` deprecated in the first place? What is wrong with that? – Suzuna Minami Aug 18 '21 at 09:16
  • 1
    @JohnBrookfields, a quick google search mentions that merely the name has changed, nothing else. This is common (like Spring changing the name of properties-placeholders from time to time). Official documentation at *4.4.1.3 Changes in the Connector/J API* from dev.mysql.com/doc/connector-j/8.0/en/connector-j-api-changes.html – Paul Sep 13 '22 at 08:12
7

I had the same problem in my Spring Boot application.
I added new parameter to my 'application.properties' file:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

And this solved my problem.

Denys Dvornyi
  • 71
  • 1
  • 5