1

Recently I have been working on a Java Web Application with JDBC and I have found that com.mysql.jdbc.Driver is deprecated since "Connector/J API 8.0".

Therefore as shown below, the Java Runtime warns you to use com.mysql.cj.jdbc.Driver, instead of using the deprecated driver.

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.

What my problem is really lies under this line.

The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

At first, I have thought this means it is not required to call Class.forName to register the driver, but after testing out, I have found that I was wrong.

So what is actually mean by manual loading of the driver class is generally unnecessary? Is there something that I have missed?

Thank you.

JudgedPluto
  • 105
  • 3
  • 10
  • Related: https://stackoverflow.com/questions/18288058/how-is-driver-class-located-in-jdbc4 – Mark Rotteveel May 12 '19 at 07:00
  • Manual loading may still be necessary if the driver is not on the initial classpath, but - for example - on the classpath of a WAR. – Mark Rotteveel May 12 '19 at 07:02
  • @MarkRotteveel sorry for bothering you, but what do you mean by "classpath of a WAR"? can you elaborate? – JudgedPluto May 12 '19 at 08:22
  • 1
    If you include a driver in the `WEB-INF/lib` of a WAR (instead of having it on the classpath of your application server itself), it will not be automatically loaded and you (or better: your data source implementation) will need to load it explicitly. – Mark Rotteveel May 12 '19 at 08:24
  • @MarkRotteveel It makes sense now. so, the solution is to include the driver in the application server's classpath (for example - `apache-tomcat-9.0.19\lib`), right? – JudgedPluto May 12 '19 at 09:17
  • Yes, that is right. – Mark Rotteveel May 12 '19 at 10:04

1 Answers1

2

The MySQL Connector/J developer guide is still referring to the Class.forName. See https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html

But starting with JDBC 4, the Service Provider Interface is used, so the call should no longer be required. (And for me it works without the Class.forName call!)

Details about the SPI:

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Konrad Neitzel
  • 736
  • 3
  • 7
  • I think that I have did something wrong on importing "Connector/J" library on Java Web Project so that I have to manually load the JDBC Driver. (I am new to Java EE and JDBC 4.0, so that's probably a reason to mess up) Nevertheless, I am making this as the correct answer, 'cause it does work on Java SE applications. Your help is appreciated, thank you. – JudgedPluto May 11 '19 at 23:06