Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7249091
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68080
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 21:48:59

下载本文示例代码
  今天用spring hibernate进行中文插入时出现乱码问题,通过查资料和自己反复测试终于解决了.  总结了两种方法:  1、使用gb2312编码,变更mysql的数据库编码字符集。cmd模式下用mysql --default-character-set=gb2312 -u root -p进入,然后再每个建表语句后增加default character set gb2312;  重新建立数据表。  值得注意的地方是:applicationContext.xml中的数据库连接必须设置为<property name="url"><value>jdbc:mysql://localhost/struts?useUnicode=true&characterEncoding=gb2312</value></property>,这样插入的才是正常的中文,否则就是乱码。   2、在进行数据保存之前进行gb2312到iso8859-1编码的转换,applicationContext.xml中的数据库连接必须设置为<property name="url"><value>jdbc:mysql://localhost/struts</value></property>,这样插入的才是正常的中文,否则就是乱码。   它们相同的地方是在用jsp进行中文内容填加时,都要进行gb2312到iso8859-1编码的转换: String name;name=trans(request.getParameter("name"));String trans(String chi){ String result = null; byte temp []; try {  temp=chi.getBytes("iso-8859-1");  result = new String(temp); } catch(java.io.UnsupportedEncodingException e) {  System.out.println (e.toString()); } return result;}String trans(Object chi){ return trans(chi.toString());}   今天用spring hibernate进行中文插入时出现乱码问题,通过查资料和自己反复测试终于解决了.  总结了两种方法:  1、使用gb2312编码,变更mysql的数据库编码字符集。cmd模式下用mysql --default-character-set=gb2312 -u root -p进入,然后再每个建表语句后增加default character set gb2312;  重新建立数据表。  值得注意的地方是:applicationContext.xml中的数据库连接必须设置为<property name="url"><value>jdbc:mysql://localhost/struts?useUnicode=true&characterEncoding=gb2312</value></property>,这样插入的才是正常的中文,否则就是乱码。   2、在进行数据保存之前进行gb2312到iso8859-1编码的转换,applicationContext.xml中的数据库连接必须设置为<property name="url"><value>jdbc:mysql://localhost/struts</value></property>,这样插入的才是正常的中文,否则就是乱码。   它们相同的地方是在用jsp进行中文内容填加时,都要进行gb2312到iso8859-1编码的转换: String name;name=trans(request.getParameter("name"));String trans(String chi){ String result = null; byte temp []; try {  temp=chi.getBytes("iso-8859-1");  result = new String(temp); } catch(java.io.UnsupportedEncodingException e) {  System.out.println (e.toString()); } return result;}String trans(Object chi){ return trans(chi.toString());} 下载本文示例代码


Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案Spring Hibernate乱码解决方案
阅读(909) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-08-05 12:33:33

SAP99,支持下,也欢迎访问我的博客, SAP资料多多 http://sap99.cublog.cn http://www.sap99.com 有很多的学习资料,推荐一下,