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

    一般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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值