初学入门:一劳永逸的数据库编码解决方案

发表于:2007-11-28 12:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

问题提出
  现在几乎所有的应用系统都无法避免使用数据库系统。在JAVA世界里访问数据库是一件非常轻松的事情,JDBC为JAVA应用程序访问数据库提供了一个统一的接口,通过使用JDBC接口开发者无需关心系统最终采用哪种数据库,因为JDBC仅仅是定义了访问几个JAVA的接口类,具体的实现是由数据库厂商提供的,这种做法其实与其他数据库连接方式例如ODBC是类似的。但是在实际的应用过程中,开发者发现离JDBC设计的初衷还是有一定距离,就比如说在存储字符串时的编码问题,我想很多开发者都会遇见这个问题,倒不是因为说解决它有什么技术方面的难度,而是它的的确确非常繁琐。我们必须在每次写入或者读出字符串的时候进行编码和反编码处理;或者说我们可以写一个方法可以进行编码处理的,但又必须在每次数据库操作的时候调用,虽然调用很简单,可是我非得这样吗?要是忘了编码那又要DEBUG了。当然你可能觉得这并没有什么,或者你可能很勤快,喜欢写大量重复的代码,可是你难道没有觉得这种繁琐的工作正在浪费你过于宝贵的青春吗?停止你的键盘输入,让我们来解决这个问题吧!
解决思路
  在传统的应用程序中数据库操作部分我们可以想象成两层,如图所示:一个是数据库的"连接池",另外一个业务数据操作层。在这里数据库的连接池是广义的,你可以把JDBC中的DriverManager也当成是连接池,具体的意思就是我们可以通过这层来获取到指定数据库的连接而不去关心它是怎么获取的。如果这个时候数据库系统(有如Informix,SQL Server)要求对字符串进行转码才能存储(例如最常见的GBK->ISO8859_1转码),那我们就必须在业务数据操作层来进行,这样有多少业务数据操作我们就要做多少编码转码的工作,太麻烦了,代码中充斥中大量重复的内容。本文提出的解决方案就是利用对获取到的数据库连接实例进行二次封装,也就是在数据库连接池与业务数据操作层之间加入了连接封装层,当然了,我们也完全可以直接将连接封装集成到数据库连接池内部。
  我们知道进行编码和转码工作都是集中在JDBC的两个接口PreparedStatement和ResultSet上进行的,主要涉及PreparedStatement的setString方法以及ResultSet的getString方法。前面我们讲过需要加入一个连接封装层来对数据库连接实例进行二次封装,但是怎么通过这个封装来改变PreparedStatement和ResultSet这两个接口的行为呢?这个问题其实也很简单,因为PreparedStatement接口必须通过Connection接口来获取实例,而ResultSet接口又必须从Statement或者PreparedStatement接口来获取实例,有了这样的级联关系,问题也就迎刃而解了。还是利用我在文章《使用JAVA动态代理实现数据库连接池》中使用的动态接口代理技术。首先我们设计Connection接口的代理类_Connection,这个代理类接管了Connection接口中所有可能获取到Statement或者PreparedStatement接口实例的方法,例如:prepareStatement和createStatement。改变这两个方法使之返回的是经过接管后的Statement或者PreparedStatement实例。通过对于Statement接口也有相应的代理类_Statement,这个代理类接管用于获取ResultSet接口实例的所有方法,包括对setString方法的接管以决定是否对字符串进行编码处理。对于接口ResultSet的接管类_ResultSet就相应的比较简单,它只需要处理getString方法即可。

  关键代码

  前面我们大概介绍了这个解决方案的思路,下面我们给出关键的实现代码包括Connection的代理类,Statement的代理类,ResultSet的代理类。这些代码是在原来关于数据库连接池实现的基础上进行扩充使之增加对自动编码处理的功能。 

_Connection.java

/*
 * Created on 2003-10-23 by Liudong
 */
package lius.pool;
import java.sql.*;
import java.lang.reflect.*;

/*
 *
 * 数据库连接的代理类
 * @author Liudong
 */
 class _Connection implements InvocationHandler{
 private Connection conn = null;
 private boolean coding = false;
 //指定是否进行字符串转码操作
 _Connection(Connection conn, boolean coding){
  this.conn = conn;
  this.coding = coding;
  initConnectionParam(this.conn);
 
 }
 
 /** 
  * Returns the conn. 
  * @return Connection 
  */
 
 public Connection getConnection() {
  Class[] interfaces = conn.getClass().getInterfaces();
  if(interfaces==null||interfaces.length==0){
   interfaces = new Class[1];
   interfaces[0] = Connection.class;
 
  }
 
  Connection conn2 = (Connection)Proxy.newProxyInstance( conn.getClass().getClassLoader(), interfaces,this);
  return conn2;
 
 }
 
 /** 
  * @see java.lang.reflect.InvocationHandler#invoke 
  */
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
  String method = m.getName();
  //调用相应的操作
  Object obj = null;
  try{
   obj = m.invoke(conn, args);
   //接管用于获取语句句柄实例的方法
   if((CS.equals(method)||PS.equals(method))&&coding)
    return new _Statement((Statement)obj,true).getStatement();
 
  } catch(InvocationTargetException e) {
   throw e.getTargetException();
  }
  return obj;
 }
 
 private final static String PS = "prepareStatement";
 private final static String CS = "createStatement";
}

 
_Statement.java 

/*
 * Created on 2003-10-23 by Liudong
 */
 
package lius.pool;
import java.sql.*;
import java.lang.reflect.*;

/**
 * 数据库语句对象实例的代理类
 * @author Liudong
 */
class _Statement implements InvocationHandler{
 private Statement statement ; //保存所接管对象的实例
 private boolean decode = false; //指定是否进行字符串转码

 public _Statement(Statement stmt,boolean decode) {
  this.statement = stmt;
  this.decode = decode;
 }
 
 /** 
  * 获取一个接管后的对象实例 
  * @return 
  */
 public Statement getStatement() {
  Class[] interfaces = statement.getClass().getInterfaces();
  if(interfaces==null||interfaces.length==0){
   interfaces = new Class[1];
   interfaces[0] = Statement.class;
  }
  Statement stmt = (Statement)Proxy.newProxyInstance(  
   statement.getClass().getClassLoader(),
   interfaces,this);
  return stmt;
 
 }
 
 /** 
  * 方法接管 
  */
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
  String method = m.getName(); //接管setString方法
  if(decode && SETSTRING.equals(method)) {
   try{
    String param = (String)args[1];
    if(param!=null)
     param = new String(param.getBytes(),"8859_1");
    return m.invoke(statement,new Object[]{args[0],param});
   } catch(InvocationTargetException e){
    throw e.getTargetException();
       
   } 
  }
 
  //接管executeQuery方法
 
  if(decode && EXECUTEQUERY.equals(method)){
   try{
    ResultSet rs = (ResultSet)m.invoke(statement,args);
    return new _ResultSet(rs,decode).getResultSet();
  
   }catch(InvocationTargetException e){
    throw e.getTargetException();
    } 
  }
 
  try{
   return m.invoke(statement, args);
  } catch(InvocationTargetException e) {
   throw e.getTargetException();
   }
 }
 //两个要接管的方法名
 
 private final static String SETSTRING = "setString";
 private final static String EXECUTEQUERY = "executeQuery";
}

 
_ResultSet.java

 

/*
 * Created on 2003-10-23 by Liudong
 */
 
package lius.pool;
import java.sql.ResultSet;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 数据库结果集的代理类
 * @author Liudong
 */
 class _ResultSet implements InvocationHandler{
 private ResultSet rs = null;
 private boolean decode = false;
 
 public _ResultSet(ResultSet rs,boolean decode) {
  this.rs = rs;
  this.decode = decode;
 }
 
 public ResultSet getResultSet(){
  Class[] interfaces = rs.getClass().getInterfaces();
  if(interfaces==null||interfaces.length==0){
   interfaces = new Class[1];
   interfaces[0] = ResultSet.class; 
  }
 
  ResultSet rs2 = (ResultSet)Proxy.newProxyInstance(rs.getClass().getClassLoader(),interfaces,this);
  return rs2;
 
 }

 /** 
  * 结果getString方法 
  */
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
  String method = m.getName();
  if(decode && GETSTRING.equals(method)){
   try{
    String result = (String)m.invoke(rs,args);
    if(result!=null)    
     return new String(result.getBytes("8859_1"));
    return null;
  
   }catch(InvocationTargetException e){
    throw e.getTargetException();
    }
  
  }
 
  try{
   return m.invoke(rs, args);
  }catch(InvocationTargetException e){
   throw e.getTargetException();
  }
 }
 
 private final static String GETSTRING = "getString";

}

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号