论坛首页 Java企业应用论坛

[原创]Oracle插入中文乱码的简单解决办法

浏览 7915 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-18  
最近被这个问题困扰了好几天,项目使用spring1.2+hibernate3+struts1.3.8,今天经过牛人指点终于搞了这个问题,废话也不多说了,代码贴上来大家一看就是明白了:
首先定义一个GBKString替换默认的类型,在映射文件中使用该类型做为TYPE。
GBKString.java
package com.nl.tsp.dao;

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;

/**
 * @description
 * @author Jason Tseng
 * 
 */
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;
	}
}

Role.java
package com.nl.tsp.base;

import java.util.Map;

/**
 * @description 角色定义表
 * @author Jason Tseng
 * @date 2007-5-25
 * 
 * @hibernate.class table="SysRoleInfo" lazy="false"
 */
public class Role {

	/**
	 * 角色编号
	 * 
	 * @hibernate.id generator-class="native" column="roleInfoId"
	 */
	private Long roleId;

	/**
	 * 角色名称
	 * 
	 * @hibernate.property column="roleInfoName" type="com.nl.tsp.dao.GBKString"
	 */
	private String roleName;

	/**
	 * 功能树XML
	 * 
	 * @hibernate.property column="funcs"
	 */
	private String funcs;

	public Long getRoleId() {
		return roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getFuncs() {
		return funcs;
	}

	public void setFuncs(String funcs) {
		this.funcs = funcs;
	}

}

这种方法仅限在model层用hibernate的情况,这样就省在输入、输出前自己再手工一个个转码。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics