Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'

前端 未结 20 2426
予麋鹿
予麋鹿 2020-12-13 06:02

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         


        
相关标签:
20条回答
  • 2020-12-13 06: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");
    
    0 讨论(0)
  • 2020-12-13 06:28

    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.

    0 讨论(0)
  • 2020-12-13 06:28

    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.

    0 讨论(0)
  • 2020-12-13 06:29

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

    0 讨论(0)
  • 2020-12-13 06:30

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

    0 讨论(0)
  • 2020-12-13 06:30

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

    0 讨论(0)
提交回复
热议问题