/**
* Statement的自封裝
* 目的適記錄SQl的動作和設置相應連接最後活動時間
* @author Liudong
*/
package com
import java
import java
import java
import java
import java
import sun
public class StatementObject implements InvocationHandler{
private Statement stm=null;
private Statement stm_proxy=null;
private final static String GETRESULTSET_METHOD_NAME =
private final static String EXECUTEQUERY_METHOD_NAME =
private final static String GETGENERATEDKEYS_METHOD_NAME =
private ResultSetObject rso=null;
public StatementObject(Statement stm){
this
}
public Statement getStatement(){
if(stm_proxy==null){
ClassLoader classloader=stm
Class[] interfaces = stm
if(interfaces==null||interfaces
interfaces = new Class[
interfaces[
}
try{
stm_proxy= (Statement)Proxy
}catch(NullPointerException e){
log(e
}
if(stm_proxy!=null)
log(
}
return stm_proxy;
}
public Object invoke(Object proxy
{
Object obj = null;
log(
//判斷是否調用了getResultSet or executeQuery or getGeneratedKeys
//是就截獲
if(GETRESULTSET_METHOD_NAME
|| EXECUTEQUERY_METHOD_NAME
|| GETGENERATEDKEYS_METHOD_NAME
ResultSet rs=(ResultSet)m
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rso
}else if(rso!=null)
obj=rso
else
log(
}else{
obj = m
}
//設置最後一次訪問時間
setLastAccessTime( new Date()
return obj;
}
/**
* 將文本信息寫入日志文件
*/
private void log(String msg) {
ConnectionManager
}
/**
* 將文本信息與異常寫入日志文件
*/
private void log(Throwable e
ConnectionManager
}
//設置最後一次訪問時間
private void setLastAccessTime(long ltime){
ConnectionObject
}
}
/////////////////////////////////////////////////////////////////////////////////
/**
* PreparedStatement 的自封裝
* 目的適記錄SQl的動作和設置相應連接最後活動時間
* @author Liudong
*/
package com
import java
import java
import java
import java
import java
public class PreparedStatementObject implements InvocationHandler{
private PreparedStatement ps=null;
private PreparedStatement ps_proxy =null;
private final static String EXECUTEQUERY_METHOD_NAME =
private ResultSetObject rso=null;
PreparedStatementObject(PreparedStatement ps){
this
}
public PreparedStatement getPreparedStatement(){
if(ps_proxy==null){
ClassLoader classloader=ps
Class[] interfaces = ps
if(interfaces==null||interfaces
interfaces = new Class[
interfaces[
}
try{
ps_proxy= (PreparedStatement) Proxy
}catch(NullPointerException e){
log(e
}
if(ps_proxy!=null)
log(
}
return ps_proxy;
}
public Object invoke(Object proxy
{
Object obj = null;
log(
//是否調用了executeQuery 如果是 則接管
if(EXECUTEQUERY_METHOD_NAME
ResultSet rs=(ResultSet)m
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rso
}else if(rso!=null)
obj=rso
else
log(
}else{
obj = m
}
obj = m
//設置最後一次訪問時間
setLastAccessTime( new Date()
return obj;
}
/**
* 將文本信息寫入日志文件
*/
private void log(String msg) {
ConnectionManager
}
/**
* 將文本信息與異常寫入日志文件
*/
private void log(Throwable e
ConnectionManager
}
//設置最後一次訪問時間
private void setLastAccessTime(long ltime){
ConnectionObject
}
}
////////////////////////////////////////////////////////////////////////
/**
* Statement的自封裝
* 目的適記錄SQl的動作和設置相應連接最後活動時間
* @author Liudong
*/
package com
import java
import java
import java
import java
import java
public class ResultSetObject implements InvocationHandler{
private ResultSet rs=null;
private ResultSet rs_proxy =null
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26864.html