0 0

急求 使用代理出现类型转换异常!!! 25

具体代码如下,异常出现在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;
}


}


问题补充:
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();  
    String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=Test";  
    Connection conn = DriverManager.getConnection(url, "sa", "123456");  
 

// Connection con = getConnection();
_Connection _con = new _Connection(conn,true);
Connection cs = _con.getConnection();
PreparedStatement ps = cs.prepareStatement("select * from testuser");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.print(rs.getString(1) + "\n");
System.out.print(rs.getString(2) + "\n");
}

cs.close();
}
其他的除了 obj = m.invoke(conn,args);修改外都一样。既然楼下测试没有问题,我想是否是我的环境中少些东西。当我不用代理时候,一切连接正常。当调用Connection cs = _con.getConnection();就会报异常,Debug 时候在Thread.dispatchUncaughtExcetpion(Throwable) line:not available 页中显示Source not found.


问题补充:
根据楼下的要求 我贴出了main方法,问题还是存在
DAO 
2008年7月21日 20:40

4个答案 按时间排序 按投票排序

0 0

修改方法: [我没有测, 你试下]

public Connection getConnection() 
{ 
//返回数据库连接conn的接管类,以便截住close方法 
Connection conn2 = (Connection)Proxy.newProxyInstance( 
conn.getClass().getClassLoader(), 
Connection.class,this); // 这里改下.. conn.getClass().getInterfaces()
return conn2; 
} 



public classs com.microsoft.sqlserver.jdbc.SQLServerConnection implements java.sql.Connection, java.io.Serializable

public calss com.mysql.jdbc.Connection extends com.mysql.jdbc.ConnectionProperties implements java.sql.Connection

他们的区别在于SQLServerConnection多实现了一个接口.

2008年7月22日 14:42
0 0

除了被简单修改过的地方,其他都对,我的main()也能跑通,查询到数据.

2008年7月21日 23:55
0 0

我的测试main()

	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String url = "jdbc:mysql://localhost/iwoo?useUnicode=true&characterEncoding=utf8";
		Connection conn = DriverManager.getConnection(url, "root", null);

		_Connection _connection = new _Connection(conn, true);
		Connection proxyConn = _connection.getConnection();

		Statement s = proxyConn.createStatement();
		ResultSet r = s.executeQuery("select * from CODEMSTR");
		while (r.next()) {
			System.out.println(r.getString("CODE_ID"));
		}

		proxyConn.close();
	}

2008年7月21日 23:54
0 0

你没有把你的main()贴出来.

	public Object invoke(Object proxy, Method m, Object[] args) {
		Object obj = null;
		if (CLOSE_METHOD_NAME.equals(m.getName())) {
			setInUse(false);
		} else {
			try {
                                    // obj = 这里被我修改过了
				obj = m.invoke(conn, args);
				////设置最后一次访问时间,以便及时清除超时的连接 
				lastAccessTime = System.currentTimeMillis();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

2008年7月21日 23:53

相关推荐

Global site tag (gtag.js) - Google Analytics