100

This is the Warning im getting in console, Im confused with this warning:

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.
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
vicky
  • 1,021
  • 2
  • 7
  • 7

26 Answers26

107

I resolved this problem by change application.properties of

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

to

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

Hope it help

Wei Chun
  • 1,263
  • 2
  • 10
  • 19
28

According Changes in the Connector/J API "The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated."

This means that you just need to change the name of the driver:

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

to

Class.forName("com.mysql.cj.jdbc.Driver");
Dmytro Sokolyuk
  • 966
  • 13
  • 13
27

If you're using Hibernate then change in your "hibernate.cfg.xml" the following:

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

To:

<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

That should do :)

Yogev
  • 1,333
  • 11
  • 8
9

Change driver property in your ORM config file from

 <property name="driver" value="com.mysql.jdbc.Driver"/>

to

<property name="driver" value="com.mysql.cj.jdbc.Driver"/>

This will resolve the warning :-)

N. Girijah
  • 121
  • 1
  • 4
9

The sentence "Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver' " is clear. You should use the newer driver, like this:

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

And in mysql-connector-java-8.0.17. You would find that Class com.mysql.jdbc.Driver doesn't provide service any more. (You also can found the warning came from here.)

public class Driver extends com.mysql.cj.jdbc.Driver {
    public Driver() throws SQLException {
    }

    static {
        System.err.println("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.");
    }
}

The sentence 'The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.' It mean that write code like this is ok:

//Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?useSSL=false&serverTimezone=Asia/Shanghai","root","root");

Due to SPI, driver is automatically registered. How does it work? You can find this from java.sql.DriverManager:

private static void ensureDriversInitialized() {
                          ...
    ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
                          ...
}

And in your mysql-connector-java-XXX.jar, you also can find the file 'java.sql.Driver' in the META-INF\services. The file as follows:

com.mysql.cj.jdbc.Driver

When you run DriverManager.getConnection(), the static block also start running. So driver can be automatically registered with file 'java.sql.Driver'.

And more about SPI -> Difference between SPI and API?.

chachay
  • 340
  • 2
  • 8
5

This is beacuse the version of mysql to be connected is lower than the version of the mysql driver. Many people say that com.mysql.jdbc.Driver is changed to com.mysql.cj.jdbc.Driver , although this does not solve the problem, but it should also attract attention.

Mr.tangx
  • 51
  • 1
  • 2
  • This solved it for me. Although in my case, the other error messages did say 'com.mysql.cj.jdbc' – Manaar Mar 13 '19 at 13:16
4

Please remove existing spring.datasource.driver-class-name property in the application.properties file and add the below property.

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
3
        // The newInstance() call is a work around for some
        // broken Java implementations
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  • 5
    Please, can you extend your answer with detailed explanation? This will be very useful to understand your idea. Thank you! – vezunchik Mar 18 '19 at 16:11
  • Class.forName("com.mysql.cj.jdbc.Driver").newInstance() In this code .newInstance() method is wire beetween broken implementation. This is link is more helpful [https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html] @vezunchik – Sonu patel May 31 '19 at 09:06
3

Changed my application.conf file as below. It solved the problem.

Before Change:

slick {
  dbs {
    default {
      profile = "slick.jdbc.MySQLProfile$"
      db {
        driver = "com.mysql.jdbc.Driver"
        url = "jdbc:mysql://localhost:3306/test"
        user = "root"
        password = "root"
      }
    }
  }
}

After Change:

slick {
  dbs {
    default {
      profile = "slick.jdbc.MySQLProfile$"
      db {
        **driver = "com.mysql.cj.jdbc.Driver"**
        url = "jdbc:mysql://localhost:3306/test"
        user = "root"
        password = "root"
      }
    }
  }
}
2

in my case, I had a line Class.forName("com.mysql.jdbc.Driver"); after removing this line code works fine if you have any line for loading "com.mysql.jdbc.Driver" remove it, it doesn't require any more

2

By changing the driver name from "com.mysql.jdbc.Driver" to "com.mysql.cj.jdbc.Driver" will solve this problem.

In case of simple JDBC connection : Class.forName("com.mysql.cj.jdbc.Driver");

In case of hibernate : <property name="driver" value="com.mysql.cj.jdbc.Driver"/>

rk_stack
  • 21
  • 1
2

I'm using Eclipse and defined MySql connection pool in META_INF/context.xml. Part of its content is:

<Context>
  <Resource name="..." 
    driverClassName="com.mysql.jdbc.Driver" 
    ... />
</Context>

When I changed the line starting with "driverClassName" as follows, problem is gone.

driverClassName="com.mysql.cj.jdbc.Driver"
Park JongBum
  • 1,245
  • 1
  • 16
  • 27
  • Reproduced the same for Intellij IDEA 2020.2. This suggestion is relevant, because properties file was correct. – invzbl3 Jan 30 '21 at 01:37
2

This warning also appears if you are using the log4jdbc-spring-boot-starter library directly.

However there is a config to choose the correct driver yourself. Put this in your application.properties:

log4jdbc.drivers=com.mysql.cj.jdbc.Driver
log4jdbc.auto.load.popular.drivers=false

See documentation on Github

judos
  • 445
  • 4
  • 14
2

I only changed this line of code Class.forName("com.mysql.jdbc.Driver"); to Class.forName("com.mysql.cj.jdbc.Driver");. I see some other people going to application.properties and adding a few things. For me this is the best and fastest way but I also respect other people's answer.

furkanayilmaz
  • 154
  • 1
  • 11
2

remove line on configuration your config file application.properties

enter image description here

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

Barrrettt
  • 645
  • 7
  • 15
1

In case of using a configuration based on a YML file, the following will be the property that needs to be adjusted inside the given file:

*driverClassName: com.mysql.cj.jdbc.Driver*

jcoppens
  • 5,306
  • 6
  • 27
  • 47
ferenctth8
  • 11
  • 1
1

The driver is automatically registered via SPI and manual loading of the driver class is usually unnecessary. Just change "com.mysql.cj.jdbc.Driver"

1

If seeing this message in Hive with new MySQL connector 8.x (MySQL metastore)

open hive-site.xml and change:

   <property>
      <name>javax.jdo.option.ConnectionDriverName</name>
      <value>com.mysql.jdbc.Driver</value>
      <description>MySQL JDBC driver class</description>
   </property>

to

   <property>
      <name>javax.jdo.option.ConnectionDriverName</name>
      <value>com.mysql.cj.jdbc.Driver</value>
      <description>MySQL JDBC driver class</description>
   </property>
Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
0

my solution: org.springframework.boot 2.0.5.RELEASE

Rather: org.springframework.boot 2.1.0.RELEASE

hadama kebe
  • 51
  • 1
  • 2
0

in my experience. I was using jsp for web. at that time I use mysql 5 and mysql connecter jar 8. So due to the version problem I face this kind of problem. I solve by replace the mysql connector jar file the exact version mysql.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
lava
  • 6,020
  • 2
  • 31
  • 28
0

working example:

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_db_name?autoReconnect=true&useSSL=false", "root", "root");

call like this it will work.

Robdll
  • 5,865
  • 7
  • 31
  • 51
0

There important changes to the Connector/J API going from version 5.1 to 8.0. You might need to adjust your API calls accordingly if the version you are using falls above 5.1.

please visit MySQL on the following link for more information https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-api-changes.html

0

just delete this part Class.forName("com.mysql.jdbc.Driver") from your code

because the machine is throwing a warning that

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

meaning that no need to include it beacuse the driver is automatically registered for you by default.

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
Tabu Taia
  • 1
  • 1
  • 2
0

If you have this in your application.properties:

spring.datasource.driverClassName=com.mysql.jdbc.Driver,

you can get rid of the error by removing that line.

ACV
  • 9,964
  • 5
  • 76
  • 81
0

Now you database connection create according to the this format.

public void Connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/JavaCRUD","root","");
        }catch(ClassNotFoundException ex) {
            
        } catch (SQLException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }

Edit this code like this.

    public void Connect() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/JavaCRUD","root","");
        }catch(ClassNotFoundException ex) {
            
        } catch (SQLException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }

Now it will execute.

0
 Class.forName("com.mysql.jdbc.Driver"); //it's old the mysql driver 
 

changes old to new driver -------------------------

 Class.forName("com.mysql.cj.jdbc.Driver"); //it's new the mysql driver and paste same place and resolved your problem 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 16 '21 at 15:03
  • Where did you change it? Where did you find this code? – horvoje Jan 05 '23 at 10:43