使用Hibernate向mysql数据库中插入中文,数据库中显示??乱码

来源:互联网 发布:白熊事务所装软件 编辑:程序博客网 时间:2024/05/22 07:51

 

通过网上学习,要求修改Hibernate的配置文件hibernate.cfg.xml,将原来的
jdbc:mysql://localhost:3306/shopping
改为
 jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf-8
但是又出现以下异常:
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2019)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1931)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1910)
    at action.AddProduct.(AddProduct.java:22)
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:2011)
    ... 3 more
Exception in thread "main" java.lang.NullPointerException
    at action.AddProduct.save(AddProduct.java:33)
    at action.AddProduct.main(AddProduct.java:94)
折腾半天才发现需要将&进行转码,作如下更改。
 jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf-8