The difference between com.mysql.jdbc.Driver and com.mysql.cj.jdbc.Driver

        When you connect to mysql and start the project, you will be warned that it is recommended to use com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver.

        When versions after 5 are selected com.mysql.jdbc.Driver, there will be a warning prompt, replace it withcom.mysql.cj.jdbc.Drive

        Looking at the source code, we can see that the old version Driverinherits the new version Driver, is compatible with the old version through inheritance, and adds an alarm prompt, as follows:

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

        So what is the difference between the two?

Essential difference:

  • com.mysql.jdbc.Driver is in mysql-connector-java 5, 
  • com.mysql.cj.jdbc.Driver is mysql-connector-java 6 and above

When using com.mysql.jdbc.Driver, the configuration needs to be as follows:

driver-class-name=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

When using com.mysql.cj.jdbc.Driver, the following configuration is required:

driver-class-name=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

Notice:

        Need to specify time zone (serverTimezone=UTC) and use SSL (useSSL=false)

Also note:

        When setting the time zone, if you set serverTimezone=UTC, it will be 8 hours earlier than China time. If you are in China, you can choose Asia/Shanghai or Asia/Hongkong, and configure it like the following:

driver-class-name=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Shanghai&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

Situation analysis:

If you are using maven version 6 or above of the mysql driver:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

This is using the 8.0.16 version of the Mysql driver, so the following error will be reported:

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.

Translation of the error reported above:

正在加载类'com.mysql.jdbc.Driver'。 这已被弃用。 新的
驱动程序类是'com.mysql.cj.jdbc.Driver'。
驱动程序通过SPI自动注册
并且通常不需要手动加载驱动程序类。
这时候你就要把com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver

But after you make the changes, you will still get an error:

WARN: Establishing SSL connection without server’s identity verification is not recommended. 
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection 
must be established by default if explicit option isn’t set. 
For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. 
You need either to explicitly disable SSL by setting useSSL=false, 
or set useSSL=true and provide truststore for server certificate verification.


Translation of the error reported above:

警告:建议不要在没有服务器身份验证的情况下建立SSL连接。
根据MySQL 5.5.45 +,5.6.26+和5.7.6+要求SSL连接
如果未设置显式选项,则必须默认建立。
为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。
您需要通过设置useSSL = false显式禁用SSL,
或者设置useSSL = true并为服务器证书验证提供信任库。


If SSL verification is not required at this time, add useSSL=false after the url.

At this time, no warning will be issued.

Solve the following error when using the mysql 8.0.16 version driver:

java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone.


        This is caused by the time zone difference between the database and the system. The problem can be solved by adding serverTimezone=GMT after the URL of the jdbc connection. If you need to use the gmt+8 time zone, you need to write it as GMT%2B8, otherwise it will be parsed as empty.

        Another solution is to use a lower version of the MySQL jdbc driver. There will be no time zone problem in 5.1.28.

Reposted from

The difference between com.mysql.jdbc.Driver and com.mysql.cj.jdbc.Driver_DayFight_DayUp's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/yangyangye/article/details/131960028