大家在开发时,有的可能会遇到后台数据库的字符集不是中文字符集的时候,但你又不能去重新修改字符集。只能通过转码来实现,这里我给大家提供两种转码的方法。分别适合JDBC和HIBERNATE。
第一种JDBC的转码。
enCode()方法是往数据库里存入数据时的转码方法。deCode()方法是从数据库中取数据时的转码方法。这种方法就不多说了。
第二种是HIBERNATE的转码。下面的代码是我从网上找的。
这个类是使用了HIBERNATE的自定义类型的方式实现转码的。我先说一下这个的原理,然后在介绍何如使用它。
上面代码主要的两个方法是 nullSafeGet()方法和 nullSafeSet()方法。 nullSafeGet方法是HIBERNATE通过映射从数据库中取出数据所用的方法。而 nullSafeSet方法是往数据库存入数据,所要用的方法。
下面是它的用法
就是在HIBERNATE的映射文件(*.hbm.xml)中,用上面的类去替换String类型
原来的写法:
替换后的写法:
以前就是我在数据库转码时的一点心得。
第一种JDBC的转码。
- public static String enCode(String str) {
- if (str == null) {
- return "";
- }
- if (str.equals("") || str.equals(" ")) {
- return "";
- } else {
- byte temp[];
- temp = str.getBytes();
- try {
- str = new String(temp, "ISO-8859-1");
- } catch (Exception e) {
- System.err.println("convert error: " + e);
- }
- return str;
- }
- }
- public static String deCode(String str) {
- if (str == null) {
- return "";
- }
- if (str.equals("") || str.equals(" ")) {
- return "";
- } else {
- byte temp[];
- try {
- temp = str.getBytes("ISO-8859-1");
- str = new String(temp, "GBK");
- } catch (Exception e) {
- System.err.println("convert error: " + e);
- }
- return str;
- }
- }
第二种是HIBERNATE的转码。下面的代码是我从网上找的。
- import java.io.Serializable;
- import java.io.UnsupportedEncodingException;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import oracle.jdbc.driver.OracleTypes;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- import org.hibernate.HibernateException;
- import org.hibernate.usertype.UserType;
- public class GBKString implements UserType {
- public GBKString() {
- super();
- }
- public int[] sqlTypes() {
- return new int[] { OracleTypes.VARCHAR };
- }
- public Class returnedClass() {
- return String.class;
- }
- public boolean equals(Object x, Object y) throws HibernateException {
- return (x == y) || (x != null && y != null && (x.equals(y)));
- }
- public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
- throws HibernateException, SQLException {
- String val = rs.getString(names[0]);
- if (null == val) {
- return null;
- } else {
- try {
- val = new String(val.getBytes("iso-8859-1"), "GBK");
- } catch (UnsupportedEncodingException e) {
- throw new HibernateException(e.getMessage());
- }
- return val;
- }
- }
- public void nullSafeSet(PreparedStatement st, Object value, int index)
- throws HibernateException, SQLException {
- if (value == null) {
- st.setNull(index, OracleTypes.VARCHAR);
- } else {
- String val = (String) value;
- try {
- val = new String(val.getBytes("GBK"), "iso-8859-1");
- } catch (UnsupportedEncodingException e) {
- throw new HibernateException(e.getMessage());
- }
- st.setObject(index, val, OracleTypes.VARCHAR);
- }
- }
- public Object deepCopy(Object value) throws HibernateException {
- if (value == null)
- return null;
- return new String((String) value);
- }
- public boolean isMutable() {
- return false;
- }
- public Object assemble(Serializable arg0, Object arg1)
- throws HibernateException {
- // TODO Auto-generated method stub
- return null;
- }
- public Serializable disassemble(Object arg0) throws HibernateException {
- // TODO Auto-generated method stub
- return null;
- }
- public int hashCode(Object arg0) throws HibernateException {
- return HashCodeBuilder.reflectionHashCode(this);
- }
- public Object replace(Object arg0, Object arg1, Object arg2)
- throws HibernateException {
- // TODO Auto-generated method stub
- return null;
- }
- }
上面代码主要的两个方法是 nullSafeGet()方法和 nullSafeSet()方法。 nullSafeGet方法是HIBERNATE通过映射从数据库中取出数据所用的方法。而 nullSafeSet方法是往数据库存入数据,所要用的方法。
下面是它的用法
就是在HIBERNATE的映射文件(*.hbm.xml)中,用上面的类去替换String类型
原来的写法:
- <property
- name="materialCode"
- type="java.lang.String"
- update="true"
- insert="true"
- column="MATERIAL_CODE"
- />
- <property
- name="materialCode"
- type="com.lhsm.core.util.GBKString"
- update="true"
- insert="true"
- column="MATERIAL_CODE"
- />