mysql8 JDBC连接注意事项
jdbc连接mysql8以上需要注意的问题:
1、maven依赖需要指定8.0以上
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
2、加载驱动应该使用 com.mysql.cj.jdbc.Driver, 如果依旧使用 com.mysql.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. java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.song.mybatis.jdbc.jdbcTest.main(jdbcTest.java:27)
在报错提示中其实已经给出提示 com.mysql.jdbc.Driver 是废弃的,不能使用, 应该使用新的驱动类 com.mysql.cj.jdbc.Driver
3、在写连接路径url时需要在末尾加上时区,mysql8以上的版本新增时区这个设置,下面错误就是由于数据库和系统时区差异所造成,在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用GMT+8时区,需要写成GMT%2B8,否者会被解析为空。