连接mysql报“com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException”的错误解决

使用jdbc连接mysql数据库,报错

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException。 

百度后有人说原因是连接的mysql数据库是8.0版本,而项目使用com.mysql.jdbc.Driver驱动包是5.1版本,将项目驱动版本改成8.0.15可以解决。

于是将com.mysql.jdbc.Driver的版本改为8.0.15,重新执行代码后报错:

Exception in thread "main" 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.

这个错误是时区问题,数据库安装时默认为英语,0:00时区,Windows系统中,XP的时区是GMT,而Win7的时区是UTC。mysql返回的时间会比实际时间要早8小时。
上面的提示信息很明显,我们可以通过配置服务器或者通过serverTimeZone配置Jdbc driver连接参数来指定一个特定的时区。

(1)配置JDBC连接参数。

在url连接字符串后面加上

?serverTimezone=UTC

(2)修改MySQL数据库配置。

打开cmd  mysql -uroot -p

show variables like '%time_zone%';

mysql> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone |        |
| time_zone        | SYSTEM |
+------------------+--------+

--设置为东八区(北京时间)
set global time_zone='+8:00';

mysql> set global time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)

设置完重新打开命令行,进入mysql,查询time。

mysql> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone |        |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set, 1 warning (0.01 sec)

这时候即使jdbc不加serverTimezone=UTC,依然运行正常。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇