mysql - SPI自动注册“com.mysql.cj.jdbc.Driver”的困惑
问题描述
最近我一直在使用 JDBC 开发一个 Java Web 应用程序,我发现它com.mysql.jdbc.Driver
自“Connector/J API 8.0”以来已被弃用。
因此,如下所示,Java 运行时警告您使用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.
我的问题真正在于这条线下。
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
一开始我以为这意味着注册驱动程序不需要调用Class.forName
,但经过测试,我发现我错了。
那么实际上是什么意思manual loading of the driver class is generally unnecessary
呢?有什么我错过的吗?
谢谢你。
解决方案
MySQL 连接器/J 开发人员指南仍然引用 Class.forName。见https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html
但从 JDBC 4 开始,使用了服务提供者接口,因此不再需要调用。(对我来说,它不需要 Class.forName 调用!)
SPI的详细信息:
推荐阅读
- pandas - Pandas Dataframe 根据算术距离有条件地更改值
- c# - 始终表示数据库状态的只读 POCO
- export-to-csv - 使用海龟坐标导出世界
- c# - 如何在标签中显示计时器的剩余时间?
- java - Spring Boot 中的多播 Websocket
- c# - 如何使用 NUnit 在 .NET Core 中模拟 UserManager?
- python - 在数据库django中找到类似的数据
- javascript - 即使在再次销毁和初始化数据表之后,数据表也不会在更新列表时更新数据(敲除绑定)。
- vba - 带有 Addin Workbooks_Open 事件的文件中的 VBA 宏安全性
- wordpress - 在孩子中列出来自父母的所有孩子自定义帖子