Oracle 数据库转码 US7ASCII ZHS16GBK

Oracle数据库编码一般是 ZHS16GBK,但是 遇到一个Oracle数据库是 US7ASCII编码,在显示中文的时候遇到了问题,出现乱码下面给出如下解决方式:

步骤一:读取编码的配置文件


public class SystemConfig {
	private final static String PROPERTIES_NAME = "sysconfig.conf";
	
	public static String getProperties(String configName) throws UtilException{
		try{
			PropertyResourceBundle prbConfig = (PropertyResourceBundle)PropertyResourceBundle.getBundle(PROPERTIES_NAME);
			return prbConfig.getString(configName);
		}catch(Exception e){
			throw new UtilException(e,"");
		}
    }
}

步骤二:编码解码

public class BeanCodingConvert {
	public final static int ISO2GBK = 1;
	public final static int GBK2ISO = 2;

	public BeanCodingConvert() {
		super();
	}

	public static void ObjectConvert(List list,int encodeType) {
		if (list == null && !(list instanceof List)) {
			return;
		}
		for (int i = 0; i < list.size(); i++) {
			Object co = list.get(i);
			if(co instanceof Object[])
				ObjectConvert((Object[])co, encodeType);
			else{
				if(co instanceof String){
					co = StringConvert((String)co, encodeType);
					list.set(i, co);
				}else{
					ObjectConvert(co,encodeType);
				}
			}
		}
	}

	public static void ObjectConvert(Iterator iter,int encodeType) {
		if (iter == null && !(iter instanceof Iterator)) {
			return;
		}
		while (iter.hasNext()) {
			Object co = iter.next();
			ObjectConvert(co,encodeType);
		}
	}
	
	public static void ObjectConvert(Object[] o,int encodeType) {
		for(int i = 0;i < o.length; i++){
			if (o[i] instanceof String)
				o[i] = StringConvert((String)o[i],encodeType);
		}
	}

	public static void ObjectConvert(Object o,int encodeType) {
		try {
			Map map = BeanUtils.describe(o);
			Set keySet = map.keySet();
			for (Iterator iter = keySet.iterator(); iter.hasNext();) {
				Object element = (Object) iter.next();

				PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(
						o, element.toString());
				Class cl = desc.getPropertyType();
				if (cl.equals(String.class)) {
					BeanUtils.setProperty(o, element.toString(),
							encodeType==ISO2GBK?ObjectCharacterConvertChinese(map.get(element)):ObjectCharacterConvertISO(map.get(element)));
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	public static String StringConvert(String s,int encodeType){
		String tmp = encodeType==ISO2GBK?ObjectCharacterConvertChinese(s):ObjectCharacterConvertISO(s);
		return tmp;
	}
	
	public static String[] StringConvert(String[] args, int encodeType){
		for(int i=0; i<args.length; i++){
			args[i] = StringConvert(args[i], encodeType);
		}
		return args;
	}

	private static String ObjectCharacterConvertChinese(Object value) {
		if (value == null && !(value instanceof String)) {
			return null;
		}
		try {
			String temp_p = (String) value;
			byte[] temp_t = temp_p.getBytes("ISO-8859-1");
			String unicode = new String(temp_t, "gbk");
			return unicode;
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	private static String ObjectCharacterConvertISO(Object value) {
		if (value == null && !(value instanceof String)) {
			return null;
		}
		try {
			String temp_p = (String) value;
			byte[] temp_t = temp_p.getBytes("gbk");
			String unicode = new String(temp_t, "ISO-8859-1");
			return unicode;
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}
}


你可能感兴趣的:(oracle,US7ASCII)