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

来源:互联网 发布:淘宝易趣网 编辑:程序博客网 时间:2024/05/16 15:43
    一般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&amp;characterEncoding=UTF-8
</property>
原创粉丝点击