包 com.mysql.jdbc.exceptions.jdbc4 在 Mysql Connector/J 中不存在 8

Package com.mysql.jdbc.exceptions.jdbc4 does not exist in Mysql Connector/J 8

mysql-connector-java 版本 5.1.x 的 Maven 依赖项升级到 8.0.x 后,编译时收到以下错误:

[ERROR] /home/dev/testproject/src/main/java/DatabaseOperation.java:20: error: package com.mysql.jdbc.exceptions.jdbc4 does not exist
[ERROR] import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;

[ERROR] /home/dev/testproject/src/main/java/DatabaseService.java:15: error: package com.mysql.jdbc.exceptions.jdbc4 does not exist
[ERROR] import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

显然 com.mysql.jdbc.exceptions.jdbc4 已被删除,迁移文档中没有任何弃用或注释。现在有什么选择?

事实上,在发行说明或迁移文档中没有提到这一点。我在 MySQL 论坛上找到了相关主题:Connector/J 8, catching primary key violations。这是 MySQL 代表的回答:

You can catch java.sql.SQLIntegrityConstraintViolationException. MySQLIntegrityConstraintViolationException was needed because of c/J 5.1 hybrid code, it does nothing specific just extends this java.sql.SQLIntegrityConstraintViolationException.

换句话说,已删除包以重用现有的 java.sql 异常 类,这是一件好事,但仍应记录在案。作为上述讨论的结果,已创建错误报告:Bug #91439 upgrade guide needs to inform of removed/refactored exceptions

当我进一步检查项目中的错误时,我发现了这 3 个异常及其 java.sql 映射:

  • com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException -> java.sql.SQLSyntaxErrorException
  • com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException -> java.sql.SQLIntegrityConstraintViolationException
  • com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException -> java.sql.SQLTransactionRollbackException

(我猜 jdbc4 包中的任何其他 MySQL 特定异常也映射到 java.sql 包中的类似 SQL 异常)