我的方法:通过研究jdk类库,可以感觉到多层处理机制在数据处理上的优越性。我们完全有可能在数据库上建立一个中间层用于字符的国际化处理,我就是这么做的。仔细研究一下jdbc操作数据库出现字符编码问题的根源,很容易发现多数情况是resultset的几个string方法在作怪,因此我们就完全可以编写一个resultset中间层进行国际化处理,源码如下:
public class i18nresultset implements resultset{ private string encoding; private resultset rs; public i18nresultset(resultset rs, string encoding) throws java.io.unsupportedencodingexception{ //检查该编码名称是否被系统支持。 "".getbytes(encoding); this.rs = rs; this.encoding = encoding; } … … //以下几个方法是进行string字符串的重编码. public string getstring(int index) throws sqlexception{ string data = null; try{ data = new string(rs.getbytes(index), encoding); }catch(java.io.unsupportedencodingexception uee){} } public string getstring(stirng field) throws sqlexception{ string data = null; try{ data = new string(rs.getbytes(field), encoding); }catch(java.io.unsupportedencodingexception uee){} } public void updatestring(int index, string value) throws sqlexception{ try{ rs.updatebytes(index, value.getbytes(encoding)); }catch(java.io.unsupportedencodingexception uee){} } public void updatestring(string field, string value) throws sqlexception{ try{ rs.updatebytes(field, value.getbytes(encoding)); }catch(java.io.unsupportedencodingexception uee){} } … …}
可以看出, 所有的string操作都使用特定编码的字节数组进行存取,这样通过定义encoding的值实现数据库存取数据编码的一致性,且encoding完全可以通过在配置信息中动态定义。
同时,上面的程序又可以解决一些固有的字符串处理问题,例如控制符如\r\n导入到数据库中很有可能被解析为\\r\\n使其不能换行,通过字节数组操作,就可以解决这个问题。这样像文章固有格式就可以完整地保留下来而不需要进行额外转换操作。
结论,通过多层处理机制使用中间层对数据库数据进行层层处理可使处理环节之间形成松耦合关系,从而可以进行有效的控制。
下面给一个使用动态代理进行字符控制的代码(原创):
package com.yipsilon.crocodile.database;import java.sql.resultset;import java.lang.reflect.invocationhandler;import java.lang.reflect.method;import java.io.unsupportedencodingexception;/** * 作者 yipsilon * 如要转载, 请通知作者 */public class i18nresultsethandler implements invocationhandler{ private resultset rs; private string encoding; public i18nresultsethandler(resultset rs, string encoding) throws unsupportedencodingexception{ this.rs = rs; "".getbytes(encoding); this.encoding = encoding; } public object invoke(object proxy, method method, object[] args) throws throwable{ string methodname = method.getname(); if(methodname.equals("getstring")){ object obj = args[0]; if(obj instanceof integer){ return decodestring(rs.getbytes(((integer)obj).intvalue()), encoding); }else{ return decodestring(rs.getbytes((string)obj), encoding); } }else if(methodname.equals("updatestring")){ object obj = args[0]; if(obj instanceof integer){ rs.updatebytes(((integer)obj).intvalue(), encodestring((string)args[1], encoding)); }else{ rs.updatebytes((string)obj, encodestring((string)args[1], encoding)); } return null; } return method.invoke(rs, args); } private string decodestring(byte[] bytes, string enc){ try{ return new string(bytes, enc); } catch(unsupportedencodingexception uee){ return new string(bytes); } } private byte[] encodestring(string str, string enc){ try{ return str.getbytes(enc); } catch(unsupportedencodingexception uee){ return str.getbytes(); } }}
使用时调用:
resultset rs = ... ; //原始的resultset结果集string encoding = "gbk"; //字符编码(resultset)proxy.newproxyinstance(rs.getclass().getclassloader(), rs.getclass().getinterfaces(), new i18nresultsethandler(rs, encoding));
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




