zoukankan      html  css  js  c++  java
  • 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,否者会被解析为空。

  • 相关阅读:
    Android 获取Bitmap方式
    o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    idea 全部报错找不到包
    idea Plugin "Maven Integration Extension" was not loaded: required plugin "Maven Integration" is disabled
    okhttp 通过网关请求服务端返回数据
    mongo 过滤查询条件后分组、排序
    OKhttp3
    linux安装consul集群
    spring cloud 订单调用用户
    WebSphere概要文件管理工具
  • 原文地址:https://www.cnblogs.com/bbsong/p/12895144.html
Copyright © 2011-2022 走看看