加载类“ com.mysql.jdbc. Driver”。这是不推荐的。新的驱动程序类是“ com.mysql.cj.jdbc. 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.
231514 次浏览

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 :)

my solution: org.springframework.boot 2.0.5.RELEASE

Rather: org.springframework.boot 2.1.0.RELEASE

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

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.

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.

        // The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

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 :-)

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.

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

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"
}
}
}
}

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");

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

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"/>

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*

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?.

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

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"

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

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>

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.

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.

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.

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

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.

 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

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.