⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connections.java

📁 Connection的代理类
💻 JAVA
字号:
package ConnectionDataBase;

import java.sql.*;
import java.lang.reflect.*;

/**
 * 
 * 数据库连接的代理类 
 * @作者:qibin
 */
public class Connections implements InvocationHandler
{
	private Connection conn = null;
	private boolean coding = false;
 
	//指定是否进行字符串转码操作
	Connections(Connection conn, boolean coding)
	{
		this.conn = conn;
		this.coding = coding;
		initConnectionParam(this.conn);
	}
 
	private void initConnectionParam(Connection connection) 
	{
		// TODO Auto-generated method stub
		
	}
	
	/**  
	  * 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 Statements((Statement)obj,true).getStatement();
	  
	  }
	  catch(InvocationTargetException e) 
	  {
	  	   throw e.getTargetException();
	  }
	  return obj;
	 }
	 
	 private final static String PS = "prepareStatement";
	 private final static String CS = "createStatement";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -