java - Hibernate连接MYSQL失败提示时区错误该怎么解决?
黄舟
黄舟 2017-04-18 09:16:14
[Java讨论组]

平台:

  • WIN10 X64 专业版。

  • IDEA 2016.1.1.

  • MYSQL 5.7.12

  • hibernate 4.2.2

具体问题:

用hibernate连接MYSQL数据库的时候提示

ERROR: 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.

提示说服务器时区设置错误,我就用 MySQL Workbench 使用show variables like '%time_zone%'; 命令查看时区,显示如下图所示:

很奇怪。。。。系统时区不显示(难道内容是中文?所以显示不出来?)。

请问该怎么解决?

以下是

hibernate.cfg.xml




    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/xk_demo
        root
        5566
        1
        org.hibernate.dialect.MySQLDialect
        thread
        true

        
        
        
        
    

和自己写的测试demo:

import com.hib.db.XkMapEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class Main {

    public static void main(String[] args) throws Exception
    {
        try{
        XkMapEntity xkMapEntity = new XkMapEntity();
        xkMapEntity.setParentId(0);
        xkMapEntity.setLevel(1);
        xkMapEntity.setParentUniqueId(1001);
        xkMapEntity.setMapUrl("f://map.jpg");
        xkMapEntity.setMapPosX(29);
        xkMapEntity.setMapPosY(34);
        xkMapEntity.setDescription("demo");

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.save(xkMapEntity);
        session.getTransaction().commit();
    }catch (Exception e){
            System.out.println(e.toString());
        }
    }
}
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
ringa_lee

connection.url 值的后面追加

?serverTimezone=UTC

IDEA DataBase 中连接不上可以用同样的办法。


题主可以在 mysql 中执行命令试下:
set global time_zone='+8:00'

伊谢尔伦

@junbaor 谢谢指点,追加?serverTimezone=UTC就能写入数据库了,但是如果是?serverTimezone=UTC+8的话就会提示如下图的错误:

最后,IDEA依然提示数据库连接不上,但是却能正常向数据库写入数据,如下图所示:

伊谢尔伦

使用 GMT+8 可用。

?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号