>>分享Java编程技术,对《Java面向对象编程》等书籍提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 29024 个阅读者 刷新本主题
 * 贴子主题:  com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定 回复文章 点赞(0)  收藏  
作者:日月光华    发表时间:2019-11-29 17:45:54     消息  查看  搜索  好友  邮件  复制  引用

com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定

com.mysql.jdbc.Driver 是 mysql-connector-java 5中的,
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的

1、JDBC连接Mysql5 com.mysql.jdbc.Driver:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

2、JDBC连接Mysql6 com.mysql.cj.jdbc.Driver, 需要指定时区serverTimezone:


driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong,例如:


driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Shanghai&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

备注:
I、如果mysql-connector-java用的6.0以上的,如下:


<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

但是你的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.

此时需要把com.mysql.jdbc.Driver 改为com.mysql.cj.jdbc.Driver
II、还有一个警告:


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(安全套接层)

扩展:
在mysql文档中发现可用的时区都在/usr/share/zoneinfo目录下,ll下发现可用的时区信息如下


lewis@lewis-dzwww:/usr/share/zoneinfo$ ll  
总用量 308  
drwxr-xr-x  21 root root  4096 5月  27 12:54 ./  
drwxr-xr-x 334 root root 12288 6月  21 15:11 ../  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Africa/  
drwxr-xr-x   6 root root  4096 5月  27 12:56 America/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Antarctica/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Arctic/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Asia/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Atlantic/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Australia/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Brazil/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Canada/  
-rw-r--r--   1 root root  2102 4月  21 02:09 CET  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Chile/  
-rw-r--r--   1 root root  2294 4月  21 02:09 CST6CDT  
-rw-r--r--   1 root root  2437 4月  21 02:09 Cuba  
-rw-r--r--   1 root root  1876 4月  21 02:09 EET  
-rw-r--r--   1 root root  1972 4月  21 02:09 Egypt  
-rw-r--r--   1 root root  3559 4月  21 02:09 Eire  
-rw-r--r--   1 root root   127 4月  21 02:09 EST  
-rw-r--r--   1 root root  2294 4月  21 02:09 EST5EDT  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Etc/  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Europe/  
-rw-r--r--   1 root root   264 4月  21 02:09 Factory  
-rw-r--r--   1 root root  3687 4月  21 02:09 GB  
lrwxrwxrwx   1 root root     2 5月  27 09:07 GB-Eire -> GB  
-rw-r--r--   1 root root   127 4月  21 02:09 GMT  
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT0 -> GMT  
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT-0 -> GMT  
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT+0 -> GMT  
lrwxrwxrwx   1 root root     3 5月  27 09:07 Greenwich -> GMT  
-rw-r--r--   1 root root  1189 4月  21 02:09 Hongkong  
-rw-r--r--   1 root root   128 4月  21 02:09 HST  
-rw-r--r--   1 root root  1190 4月  21 02:09 Iceland  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Indian/  
-rw-r--r--   1 root root  1678 4月  21 02:09 Iran  
-rw-r--r--   1 root root  4475 2月  24 17:01 iso3166.tab  
-rw-r--r--   1 root root  2265 4月  21 02:09 Israel  
-rw-r--r--   1 root root   507 4月  21 02:09 Jamaica  
-rw-r--r--   1 root root   355 4月  21 02:09 Japan  
-rw-r--r--   1 root root   237 4月  21 02:09 Kwajalein  
-rw-r--r--   1 root root 10384 2月  22 23:58 leap-seconds.list  
-rw-r--r--   1 root root   655 4月  21 02:09 Libya  
lrwxrwxrwx   1 root root    14 5月  27 09:07 localtime -> /etc/localtime  
-rw-r--r--   1 root root  2102 4月  21 02:09 MET  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Mexico/  
-rw-r--r--   1 root root   127 4月  21 02:09 MST  
-rw-r--r--   1 root root  2294 4月  21 02:09 MST7MDT  
-rw-r--r--   1 root root  2453 4月  21 02:09 Navajo  
-rw-r--r--   1 root root  2460 4月  21 02:09 NZ  
-rw-r--r--   1 root root  2057 4月  21 02:09 NZ-CHAT  
drwxr-xr-x   2 root root  4096 5月  27 12:56 Pacific/  
-rw-r--r--   1 root root  2705 4月  21 02:09 Poland  
-rw-r--r--   1 root root  3453 4月  21 02:09 Portugal  
drwxr-xr-x  19 root root  4096 5月  27 12:56 posix/  
-rw-r--r--   1 root root  3545 4月  21 02:09 posixrules  
-rw-r--r--   1 root root   414 4月  21 02:09 PRC  
-rw-r--r--   1 root root  2294 4月  21 02:09 PST8PDT  
drwxr-xr-x  19 root root  4096 5月  27 12:56 right/  
-rw-r--r--   1 root root   800 4月  21 02:09 ROC  
-rw-r--r--   1 root root   571 4月  21 02:09 ROK  
-rw-r--r--   1 root root   428 4月  21 02:09 Singapore  
drwxr-xr-x   2 root root  4096 5月  27 12:56 SystemV/  
-rw-r--r--   1 root root  2747 4月  21 02:09 Turkey  
-rw-r--r--   1 root root   127 4月  21 02:09 UCT  
lrwxrwxrwx   1 root root     4 5月  27 09:07 Universal -> Zulu  
drwxr-xr-x   2 root root  4096 5月  27 12:56 US/  
lrwxrwxrwx   1 root root     4 5月  27 09:07 UTC -> Zulu  
-rw-r--r--   1 root root  1873 4月  21 02:09 WET  
-rw-r--r--   1 root root  1528 4月  21 02:09 W-SU  
-rw-r--r--   1 root root 17533 4月  13 16:01 zone1970.tab  
-rw-r--r--   1 root root 19014 4月  13 16:01 zone.tab  
-rw-r--r--   1 root root   127 4月  21 02:09 Zulu  

可以选择东8区的Hongkong、Asia/Shanghai或者Asia/Hongkong作为参数,修改后url为

jdbc:mysql://172.xx.xx.xx:3306/test?useUnicode=true&characterEncoding=gbk&useSSL=true

&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Hongkong  


程序猿的技术大观园:www.javathinker.net
  Java面向对象编程-->多线程(上)
  JavaWeb开发-->JSP中使用JavaBean(Ⅰ)
  JSP与Hibernate开发-->第一个helloapp应用
  Java网络编程-->非阻塞通信
  精通Spring-->通过Axios访问服务器
  Vue3开发-->Vue指令
  实用类 EncodingDetect,查看一个文件的字符编码
  详细介绍float和double类型的区别
  JDK17的新特性
  BIO模型的缺陷
  JAVA锁相关知识总结
  购书咨询
  Redis安装、Redis基本数据类型、Jedis、Redis集群搭建
  靠一个HashMap的讲解打动了头条面试官
  内部类的种类和用法
  Java中的受查异常与非受查异常区别
  正则表达式范例
  java比c++强大之处JVM垃圾收集算法
  Java入门实用代码:文件重命名
  Java入门实用代码:字符串优化测试
  JDBC使用8.0驱动包连接mysql设置时区serverTimezone
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。