具体代码如下,异常出现在getConnection方法中Connection conn2 = (Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(),conn.getClass().getInterfaces(),this);
异常内容:Exception in thread "main" java.lang.ClassCastException: $Proxy0
at com.think.Handler.bind(Handler.java:24)
at com.think.TestProxy.main(TestProxy.java:9)代码:
package tutorial.DBUtil;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;public class _Connection implements InvocationHandler {
private final String CLOSE_METHOD_NAME = "close";
private Connection conn = null;
private boolean inUse = false; //数据库的忙状态private long lastAccessTime = System.currentTimeMillis();//用户最后一次访问该连接方法的时间 _Connection(Connection conn,boolean inUse)
{
this.conn = conn;
this.inUse = inUse;
}
/**
 * Returns the conn.
 * @return Connection
 */
public Connection getConnection()
{
//返回数据库连接conn的接管类,以便截住close方法
Connection conn2 = (Connection)Proxy.newProxyInstance(
conn.getClass().getClassLoader(),
conn.getClass().getInterfaces(),this);
return conn2;
}/**
 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object)
 */
public Object invoke(Object proxy,Method m,Object[] args)
{
Object obj = null;
if(CLOSE_METHOD_NAME.equals(m.getName()))
{
setInUse(false);
}else{
try {
m.invoke(conn, args);
////设置最后一次访问时间,以便及时清除超时的连接
lastAccessTime = System.currentTimeMillis();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return obj;
}
/**
 * 该方法真正的关闭了数据库的连接
 * @throws SQLException
 */
public void close() throws SQLException
{
//由于类属性conn是没有被接管的连接,因此一旦调用close方法后就直接关闭连接
conn.close();
}/**
 * Returns the inUse.
 * @return boolean
 */
public boolean isInUse() {
return inUse;
}
/**
 * Returns the lastAccessTime.
 * @return long
 */
public long getLastAccessTime() {
return lastAccessTime;
}/**
 * Sets the inUse.
 * @param inUse The inUse to set
 */
public void setInUse(boolean inUse) {
this.inUse = inUse;
}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【wisdom_qr】截止到2008-07-19 23:42:21的历史汇总数据(不包括此帖):
    发帖的总数量:1                        发帖的总分数:20                       每贴平均分数:20                       
    回帖的总数量:2                        得分贴总数量:2                        回帖的得分率:100%                     
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:1                        未结的总分数:20                       
    结贴的百分比:0.00  %               结分的百分比:0.00  %                  
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html