加载类`com.mysql.jdbc.Driver'。这已被弃用。新的驱动程序类是`com.mysql.cj.jdbc.Driver'。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52032739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
提问by vicky
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.
加载类com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver'。驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类。
回答by Wei Chun
I resolved this problem by change application.properties
of
我通过改变解决了这个问题application.properties
的
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
to
到
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Hope it help
希望有帮助
回答by Yogev
If you're using Hibernate then change in your "hibernate.cfg.xml" the following:
如果您使用的是 Hibernate,请在“hibernate.cfg.xml”中更改以下内容:
<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 :)
那应该做:)
回答by Dmitry Sokolyuk
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.Driverto com.mysql.cj.jdbc.Driver. The old class name has been deprecated."
根据Connector/J API 中的更改“MySQL Connector/J 中实现 java.sql.Driver 的类的名称已从com.mysql.jdbc.Driver更改为com.mysql.cj.jdbc.Driver。旧类名称已被弃用。”
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");
回答by N. Girijah
Change driver property in your ORM config file from
将 ORM 配置文件中的驱动程序属性从
<property name="driver" value="com.mysql.jdbc.Driver"/>
to
到
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
This will resolve the warning :-)
这将解决警告:-)
回答by Mr.tangx
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.
这是因为要连接的mysql的版本低于mysql驱动的版本。很多人说将 com.mysql.jdbc.Driver 改成 com.mysql.cj.jdbc.Driver ,虽然这并不能解决问题,但也应该引起注意。
回答by Xiu
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:
这句话“加载类'com.mysql.jdbc.Driver'。这已被弃用。新的驱动程序类是'com.mysql.cj.jdbc.Driver'”很清楚。您应该使用较新的驱动程序,如下所示:
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.)
在 mysql-connector-java-8.0.17 中。你会发现 com.mysql.jdbc.Driver 类不再提供服务了。(你也可以发现警告来自这里。)
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:
“驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类”这句话。这意味着编写这样的代码是可以的:
//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:
由于 SPI,驱动程序会自动注册。它是如何工作的?你可以从 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:
在您的 mysql-connector-java-XXX.jar 中,您还可以在 META-INF\services 中找到文件“java.sql.Driver”。该文件如下:
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'.
当您运行 DriverManager.getConnection() 时,静态块也开始运行。所以驱动程序可以自动注册到文件'java.sql.Driver'。
And more about SPI -> Difference between SPI and API?.
以及更多关于 SPI -> SPI 和 API 之间的区别?.
回答by rk_stack
By changing the driver name from "com.mysql.jdbc.Driver" to "com.mysql.cj.jdbc.Driver" will solve this problem.
通过将驱动程序名称从“com.mysql.jdbc.Driver”更改为“com.mysql.cj.jdbc.Driver”将解决此问题。
In case of simple JDBC connection :
Class.forName("com.mysql.cj.jdbc.Driver");
如果是简单的 JDBC 连接:
Class.forName("com.mysql.cj.jdbc.Driver");
In case of hibernate :
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
在休眠的情况下:
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
回答by Riflan Ahmed
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
回答by Jeberdson Abraham
Changed my application.conffile as below. It solved the problem.
更改我的application.conf文件如下。它解决了这个问题。
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"
}
}
}
}
回答by Dipendra Kumar
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.
像这样调用它会起作用。