关于乱码问题的解决方法

大家在开发时,有的可能会遇到后台数据库的字符集不是中文字符集的时候,但你又不能去重新修改字符集。只能通过转码来实现,这里我给大家提供两种转码的方法。分别适合JDBC和HIBERNATE。
第一种JDBC的转码。
  1. public static String enCode(String str) {
  2.         if (str == null) {
  3.             return "";
  4.         }
  5.         if (str.equals("") || str.equals(" ")) {
  6.             return "";
  7.         } else {
  8.             byte temp[];
  9.             temp = str.getBytes();
  10.             try {
  11.                 str = new String(temp, "ISO-8859-1");
  12.             } catch (Exception e) {
  13.                 System.err.println("convert error: " + e);
  14.             }
  15.             return str;
  16.         }
  17.     }
  18.     public static String deCode(String str) {
  19.         if (str == null) {
  20.             return "";
  21.         }
  22.         if (str.equals("") || str.equals(" ")) {
  23.             return "";
  24.         } else {
  25.             byte temp[];
  26.             try {
  27.                 temp = str.getBytes("ISO-8859-1");
  28.                 str = new String(temp, "GBK");
  29.             } catch (Exception e) {
  30.                 System.err.println("convert error: " + e);
  31.             }
  32.             return str;
  33.         }
  34.     }
enCode()方法是往数据库里存入数据时的转码方法。deCode()方法是从数据库中取数据时的转码方法。这种方法就不多说了。
第二种是HIBERNATE的转码。下面的代码是我从网上找的。
  1. import java.io.Serializable;
  2. import java.io.UnsupportedEncodingException;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import oracle.jdbc.driver.OracleTypes;
  7. import org.apache.commons.lang.builder.HashCodeBuilder;
  8. import org.hibernate.HibernateException;
  9. import org.hibernate.usertype.UserType;
  10. public class GBKString implements UserType {
  11.     public GBKString() {
  12.         super();
  13.     }
  14.     public int[] sqlTypes() {
  15.         return new int[] { OracleTypes.VARCHAR };
  16.     }
  17.     public Class returnedClass() {
  18.         return String.class;
  19.     }
  20.     public boolean equals(Object x, Object y) throws HibernateException {
  21.         return (x == y) || (x != null && y != null && (x.equals(y)));
  22.     }
  23.     public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
  24.             throws HibernateException, SQLException {
  25.         String val = rs.getString(names[0]);
  26.         if (null == val) {
  27.             return null;
  28.         } else {
  29.             try {
  30.                 val = new String(val.getBytes("iso-8859-1"), "GBK");
  31.             } catch (UnsupportedEncodingException e) {
  32.                 throw new HibernateException(e.getMessage());
  33.             }
  34.             return val;
  35.         }
  36.     }
  37.     public void nullSafeSet(PreparedStatement st, Object value, int index)
  38.             throws HibernateException, SQLException {
  39.         if (value == null) {
  40.             st.setNull(index, OracleTypes.VARCHAR);
  41.         } else {
  42.             String val = (String) value;
  43.             try {
  44.                 val = new String(val.getBytes("GBK"), "iso-8859-1");
  45.             } catch (UnsupportedEncodingException e) {
  46.                 throw new HibernateException(e.getMessage());
  47.             }
  48.             st.setObject(index, val, OracleTypes.VARCHAR);
  49.         }
  50.     }
  51.     public Object deepCopy(Object value) throws HibernateException {
  52.         if (value == null)
  53.             return null;
  54.         return new String((String) value);
  55.     }
  56.     public boolean isMutable() {
  57.         return false;
  58.     }
  59.     public Object assemble(Serializable arg0, Object arg1)
  60.             throws HibernateException {
  61.         // TODO Auto-generated method stub
  62.         return null;
  63.     }
  64.     public Serializable disassemble(Object arg0) throws HibernateException {
  65.         // TODO Auto-generated method stub
  66.         return null;
  67.     }
  68.     public int hashCode(Object arg0) throws HibernateException {
  69.         return HashCodeBuilder.reflectionHashCode(this);
  70.     }
  71.     public Object replace(Object arg0, Object arg1, Object arg2)
  72.             throws HibernateException {
  73.         // TODO Auto-generated method stub
  74.         return null;
  75.     }
  76. }
这个类是使用了HIBERNATE的自定义类型的方式实现转码的。我先说一下这个的原理,然后在介绍何如使用它。
上面代码主要的两个方法是 nullSafeGet()方法和 nullSafeSet()方法。 nullSafeGet方法是HIBERNATE通过映射从数据库中取出数据所用的方法。而 nullSafeSet方法是往数据库存入数据,所要用的方法。
下面是它的用法
就是在HIBERNATE的映射文件(*.hbm.xml)中,用上面的类去替换String类型
原来的写法:
  1.  <property
  2.             name="materialCode"
  3.             type="java.lang.String"
  4.             update="true"
  5.             insert="true"
  6.             column="MATERIAL_CODE"
  7.         />
替换后的写法:
  1.  <property
  2.             name="materialCode"
  3.             type="com.lhsm.core.util.GBKString"
  4.             update="true"
  5.             insert="true"
  6.             column="MATERIAL_CODE"
  7.         />
以前就是我在数据库转码时的一点心得。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值