首页 > 学院 > 开发设计 > 正文

hibernate的中文问题的解决方案

2019-11-18 16:19:03
字体:
来源:转载
供稿:网友

  系统配置: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文件的<session-factory/>块中设置
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">UTF-8</property>
    或编程时写:
      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

(出处:http://www.VeVb.com)



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表