站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题hibernate的中文问题的解决方案

hibernate的中文问题的解决方案

摘要:
系统配置:win2k3 server,jsdk 1.5.0 rc,mysql 4.0.20a,hibernate 2.1.0 ,elcipse 3.0.1
问题描述:在使用hibernate作为数据持久层的方案时,照样会遇到中文问题,具体情况时插入到数据库中的汉字显示为??,显示数据时汉字为??
探索: 经过试验发现与汉字编码有关的地方有以下两处,一个是数据库连接url ,一个是编程时获取Configuration 类的实例的语句的方式。
方案1:
* 编程时获取Configuration 类的实例的语句为
* new Configuration().addClass(xx.class);
必须在hibernate.properties文件里数据库连接url后加上? characterEncoding=gbk&useUnicode=true
方案2:
* 编程时获取Configuration 类的实例的语句为
* new Configuration().configure().
在hibernate.cfg.xml文件的块中设置
true
UTF-8
或编程时写:
Properties extraProperties = new Properties();
extraProperties.put("hibernate.connection.useUnicode", "true");
extraProperties.put("hibernate.connection.characterEncoding", "UTF-8");
myConfiguration.addProperties(extraProperties);

原来怀疑与xml页面的编码有关试了一下,好像不起作用。

以上参考了 http://forum.javaeye.com/viewtopic.php?t=5497
↑返回目录
前一篇: 如何使用WebSphere 6.0 Rapid Development and
后一篇: Java中关于XML的API一瞥