mysql-connector-java:6.0.6连接jdbc出错的解决方案

来源:互联网 发布:读读日报 关注知乎日报 编辑:程序博客网 时间:2024/05/18 01:48

mysql-connector-java:6.0.6连接jdbc出错的解决方案

今天沿用过去连接mysql jdbc的配置时报错了,mysql的6.0.6的包和过去的配置不太一样,主要是以下两个原因:

  • mysql-connector包使用了新的驱动

  • 连接的URL中需要增加时区信息


错误描述

1.mysql-connector包使用了新的驱动

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.

'com.mysql.jdbc.Driver'驱动类被弃用了,新的驱动类是'com.mysql.cj.jdbc.Driver'
这个驱动会通过SPI的方式自动加载,通常不需要人工手动加载

SPI – Service Provider Interface(服务提供接口)

2.连接的URL中需要增加时区信息

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.

服务器的时区值’�й���׼ʱ��’ (就是乱码,可能因为没有设置一个值)不能被识别,或者该值代表了多个时区。
如果你想获得时区支持的功能,你需要用一个更具体的值来设置服务器或JDBC驱动(通过serverTimezone 属性设置)

另外还有一个警告:

WARN: Establishing SSL connection without server’s identity verification is not recommended.
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection
must be established by default if explicit option isn’t set.
For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’.
You need either to explicitly disable SSL by setting useSSL=false,
or set useSSL=true and provide truststore for server certificate verification.

不推荐不使用服务器身份验证来建立SSL连接。
如果未明确设置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接。
为了符合当前不使用SSL连接的应用程序,verifyServerCertificate属性设置为’false’。
如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接
如果你需要用SSL连接,就要为服务器证书验证提供信任库,并设置useSSL=true。

SSL – Secure Sockets Layer(安全套接层)


解决方案

其实报错信息已经很详细的指出了问题的原因,所以根据报错信息解决就行了。

1.mysql-connector包使用了新的驱动

解决方法1:jdbc.driver的属性值从com.mysql.jdbc.Driver换为com.mysql.cj.jdbc.Driver.
解决方法2:在代码中去掉Class.forName(driver);,因为驱动会自动加载。

2.连接的URL中需要增加时区信息

增加serverTimezone属性并设置时区值,测试UTC(世界标准时间)和GMT(格林威治时间)都可以。

jdbc.url=jdbc:mysql://localhost:3306/darkbright?serverTimezone=GMT

3.关于SSL连接的Warning

再在URL后面添加一个属性useSSL,并设置为false

jdbc.url=jdbc:mysql://localhost:3306/darkbright?serverTimezone=GMT&useSSL=false

未解决的问题

  1. 添加时区信息除了解决报错还有什么用,所谓的Time zone support是什么。
  2. 如果不关闭SSL连接,那如何进行身份验证。
阅读全文
0 0
原创粉丝点击