数据库:动态代理

第一步:prop.properties配置文件

className = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/test
user = root
password =********

第二步:写工具类,中使用动态代理

package com.liuzhen.proxy;

import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

import javax.sql.DataSource;

import com.liuzhen.util.JdbcUtil;

public class MyDataSource implements DataSource {

	private static String className;
	private static String url;
	private static String user;
	private static String password;
	private static LinkedList<Connection> pool = new LinkedList<Connection>();
	static {
		try {
			InputStream in = JdbcUtil.class.getClassLoader()
					.getResourceAsStream("prop.properties");
			Properties prop = new Properties();
			prop.load(in);
			className = prop.getProperty("className");
			user = prop.getProperty("user");
			password = prop.getProperty("password");
			url = prop.getProperty("url");
			Class.forName(className);
			// 初始化10个连接到池中
			for (int i = 0; i < 10; i++) {
				Connection conn = DriverManager.getConnection(url, user,
						password);
				pool.add(conn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public synchronized Connection getConnection() throws SQLException {

		if (pool.size() > 0) {
			final Connection conn = pool.remove();
			//使用动态代理
			return (Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn
					.getClass().getInterfaces(), new InvocationHandler() {

				@Override
				public Object invoke(Object proxy, Method method, Object[] args)
						throws Throwable {
					if("close".equals(method.getName()))
					{
						return pool.add(conn);
						
					}else
					{
						return method.invoke(conn, args);
					}
				}
			});

		} else {
			throw new RuntimeException("服务器忙!");
		}

	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		return null;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return null;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
第三步:使用操作工具类

package com.liuzhen.proxy;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

public class MyClient {

   private static DataSource ds = new MyDataSource();
	public static void main(String[] args) {

		Connection conn =null;
		Statement stmt = null;
		try
		{
			conn = ds.getConnection();
			stmt = conn.createStatement();
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			if(stmt!=null)
			{
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null)
			{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值