更换mysql-connector-java包的版本之后报错SQLNestedException: Cannot load JDBC driver classs'com.mysql.cj.jdbc.D

更换mysql-connector-java包的版本之后报错。

部分错误信息

### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.cj.jdbc.Driver'
### The error may exist in file [C:\Users\Dell\IdeaProjects\comucartrainhaoliu\target\classes\mappers\WordMapper.xml]
### The error may involve com.ucar.train.week1.hao.liu.dao.WordMapper.selectTopNWord
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.cj.jdbc.Driver'


  • 原因:更换mysql-connector包的版本后,无法与原来数据库连接配置文件中的driverClass,connectionURL匹配,导致报错。

我是从5.1.6换到8.0.16出问题的。

  • 一般来说,旧版本的mysql-connector-java使用com.mysql.jdbc包下的Driver。connectionURL也不需要设置时区。
  • 较新版本的mysql-connector-java建议使用com.mysql.cj.jdbc.Driver,而且connectionURL中要添加时区设置。
较新版本的数据库配置
db.driverClassName=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/{数据库名}?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT 

旧版本的数据库配置
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/word?characterEncoding=utf-8

你可能感兴趣的:(更换mysql-connector-java包的版本之后报错SQLNestedException: Cannot load JDBC driver classs'com.mysql.cj.jdbc.D)