jdbc连接数据库mysql的问题_JDBC连接Mysql数据库出现的问题汇总

最近安装了一个 mysql 8.0

版本的数据库,在程序中连接的时候可谓是状况不断。之前也会遇到一些问题,这里就对使用 JDBC 连接 mysql 会出现的问题做一个汇总。

在此之前说明一下环境:

开发工具:IDEA

mysql版本: 8.0.12 for Win64 on x86_64 (MySQL Community Server - GPL)

mysql驱动包:8.0.12

驱动包URL 的改变

异常信息

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.

原因

通过异常我们可以发现,新的驱动url是 com.mysql.cj.jdbc.Driver

,经过在网上查阅资料发现,从 mysql6

开始,驱动包开始使用新的驱动url。如果使用最新的 url ,会报驱动已过时的错误。

解决方法

将驱动 URL 由 com.mysql.jdbc.Driver

换成 com.mysql.cj.jdbc.Driver

SSL 警告

警告信息

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+和5.7.6+要求的SSL连接。对于不使用SSL的现有应用程序,ValuyServer证书属性设置为“false”。您需要通过设置useSSL=false来显式禁用SSL,或者设置useSSL=true并提供用于服务器证书验证的信任库`。

解决方法

一般在开发中基本不需要使用 SSL 连接,在连接字符串后添加 useSSL=false

参数就行。但是如果真的有 SSL 连接的需要,则在驱动 URL 后添加 useSSL=true

参数。

jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8&useSSL=false

时区问题

异常信息

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.

原因

同样也是由于版本升级后,新的版本 数据库 和系统之间有了时区差异,需要指定时区 serverTimezone

解决方法

连接字符串后添加参数 &serverTimezone=GMT%2B8

,最终连接字符串如下:

jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8

修改数据库时间。先通过命令行连上数据库,依次输入命令及其输出如下

mysql> show variables like "%time_zone";

+------------------+--------+

| Variable_name | Value |

+------------------+--------+

| system_time_zone | |

| time_zone | SYSTEM |

+------------------+--------+

2 rows in set, 1 warning (0.04 sec)

mysql> set global time_zone="+8:00";

Query OK, 0 rows affected (0.01 sec)

XML 配置文件中 & 的转义

异常信息

org.mybatis.generator.exception.XMLParserException: XML Parser Error on line 16: 对实体 “useSSL” 的引用必须以 ‘;’ 分隔符结尾。

原因

这是我在使用 mybatis generator

时出现的错误。当时我想在连接字符串后加上 useSSL

参数,但是由于在 XML

文件中, &

是被禁止的,所以需要使用 &

的时要用它的转义 &

来代替。

解决方法

将连接字符串中的 &

符号改成 &

详细连接字符串参考

jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=GMT%2B8&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true

当然如果是使用 XML 作为配置文件,需要将 连接字符串中的 &

符号改成 &

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值