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>
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>
相关文章推荐
- jdbc 连接 mysql 时的中文乱码问题
- jdbc 连接 mysql 时的中文乱码问题
- jdbc 连接 mysql 时的中文乱码问题
- JDBC连接MYSQL中文乱码问题 .
- 【MySQL学习笔记】4:解决JDBC连接MySQL的query结果中文乱码问题
- Hibernate连接MySQL的中文乱码问题:
- jdbc 连接 mysql 时的中文乱码问题
- jdbc 连接 mysql 时的中文乱码问题
- 中文乱码--MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总
- 关于 Hibernate3 与 Mysql5.5 连接时出现的中文乱码问题
- JDBC连接MYSQL中文乱码问题
- JDBC连接MySQL 插入中文数据的乱码问题
- jdbc连接mysql中文乱码问题
- Jdbc 连接 Mysql 时的中文乱码问题
- Hibernate连接mysql数据的中文乱码问题
- jdbc 连接 mysql 时的中文乱码问题
- C# 连接 Mysql 中文乱码问题
- Hibernate连接Mysql中文乱码处理
- jdbc连接mysql数据库出现中文乱码需要注意的问题
- Hibernate保存mysql数据后中文乱码问题