cari

Rumah  >  Soal Jawab  >  teks badan

java - Hibernate连接MYSQL失败提示时区错误该怎么解决?

平台:

具体问题:

用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

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/xk_demo</property>
        <property name="connection.username">root</property>
        <property name="connection.password">5566</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>

        <mapping class="com.hib.db.XkMapEntity"/>
        <mapping resource="com/hib/db/XkMapEntity.hbm.xml"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

和自己写的测试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());
        }
    }
}
黄舟黄舟2767 hari yang lalu1117

membalas semua(3)saya akan balas

  • ringa_lee

    ringa_lee2017-04-18 09:18:14

    Tambah connection.url selepas nilai

    ?serverTimezone=UTC

    Anda boleh menggunakan kaedah yang sama jika anda tidak dapat menyambung ke IDEA DataBase.


    Anda boleh cuba laksanakan arahan dalam mysql:
    set global time_zone='+8:00'

    balas
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:18:14

    @junbaor Terima kasih atas nasihat Jika anda menambah ?serverTimezone=UTC, ia boleh ditulis ke pangkalan data Walau bagaimanapun, jika ia ?serverTimezone=UTC+8, ia akan menggesa ralat seperti yang ditunjukkan di bawah:
    .

    Akhir sekali, IDEA masih menggesa bahawa pangkalan data tidak boleh disambungkan, tetapi ia boleh menulis data ke pangkalan data seperti biasa, seperti yang ditunjukkan dalam rajah di bawah:

    balas
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:18:14

    Tersedia menggunakan GMT+8.

    ?zeroDateTimeBehavior=convertToNull&amp;serverTimezone=GMT%2b8

    balas
    0
  • Batalbalas