首先理清几个概念:

  • JDBC:java数据库连接,是Orical公司的指定的一套规范接口
  • java数据库驱动:JDBC的实现类,由相应的数据库厂商提供,可以通过驱动去操作不同的数据库
  • 在java-数据库这里,jdbc-api中的所有包都是java.sql或者javax.sql

JDBC的操作步骤:

(1)建立数据库和表

(2)创建项目

(3)导入驱动jar包

(4)注册驱动

Class.forName("com.mysql.jdbc.Driver");

(5)获取连接

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:端口号/项目名", "登录名", "密码");

前言

最近安装了一个 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。如果使用旧的 5.0 版本的驱动包,则不用驱动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)

异常信息

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

总结

更多相关文章

  1. Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
  2. Android(安卓)10 定位问题,获取NMEA(支持5.0~10.0)
  3. mybatisplus的坑 insert标签insert into select无参数问题的解决
  4. Android系统配置数据库注释(settings.db)
  5. 关于Android(安卓)Studio3.2新建项目Android(安卓)resource link
  6. Android软键盘适配问题
  7. SlidingMenu和ActionBarSherlock结合做出出色的App布局,Facebook
  8. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  9. Android(安卓)Calendar使用过程中遇到的问题

随机推荐

  1. 如何使用与相同文件运行的命令更新文件?
  2. Linux安装ElasticSearch-2.2.0-分词器插
  3. 解决useradd 用户后没有添加用户Home目录
  4. 【RFB】Linux uinput 分析,虚拟鼠标,键盘
  5. Red Hat Linux下如何修改网卡MAC地址
  6. window下使用vnc远程登录linux图形界面和
  7. Linux源码包里有个scripts文件夹,里面放的
  8. linux驱动之分离分层的概念
  9. 在两台Linux机器之间配置一条SLIP链路,以
  10. linux下mysql表名大小写敏感问题