您的位置:首页 > 数据库 > MySQL

JDBC和Hibernate连接MySQL中文乱码问题

2013-08-26 23:11 471 查看
    一般jdbc连接时只需要将url写成如下即可:
    jdbc:mysql://127.0.0.1:3306/hello?useUnicode=true&characterEncoding=UTF-8 

    但是在使用Hibernate时,这种写法会报错:

org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:26)
at utils.HibernateUtil.<clinit>(HibernateUtil.java:16)
at dao.DetailHome.<init>(DetailHome.java:26)
at Test.main(Test.java:18)
Caused by: org.dom4j.DocumentException: Error on line 9 of document  : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with
the ';' delimiter.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
... 6 more

    于是去网上找,发现&要使用转义字符,所以在hibernate.cfg.xml中正确的配置应该是:

<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/hello?useUnicode=true&characterEncoding=UTF-8
</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息