用两种方式实现精简DataSource

DataSources是有jdk中sql提供的接口供大家使用,可更快的实现数据库连接的获取,实现数据库连接池等功能;

  1. 使用动态代理的方式事项DataSource
    package com.poweruniverse.app.job;
    
    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.sql.SQLFeatureNotSupportedException;
    import java.util.LinkedList;
    import java.util.logging.Logger;
    
    import javax.sql.DataSource;
    
    public class LiYuDataBase implements DataSource{
    	private static LinkedList<Connection> pool=new LinkedList<Connection>();
    	private static final String driver="oracle.jdbc.OracleDriver";
    	private static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    	private static final String user="knowledge";
    	private static final String password="knowledge";
        //初始化连接池
    	static{
    		try {
    			Class.forName(driver);
    			for(int i=0;i<100;i++) {
    				Connection connection = DriverManager.getConnection(url, user, password);
    				pool.add(connection);
    			}
    		} catch (ClassNotFoundException | SQLException e) {
    			e.printStackTrace();
    		}
    	}
    	@Override
    	public PrintWriter getLogWriter() throws SQLException {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@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 int getLoginTimeout() throws SQLException {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	@Override
    	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public <T> T unwrap(Class<T> iface) throws SQLException {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public boolean isWrapperFor(Class<?> iface) throws SQLException {
    		// TODO Auto-generated method stub
    		return false;
    	}
    //使用代理对象获取数据库连接
    	@Override
    	public Connection getConnection() throws SQLException {
    		if(!pool.isEmpty()){
    			final Connection connection = pool.removeFirst();
    			return  (Connection)Proxy.newProxyInstance(connection.getClass().getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() {
    				@Override
    				//proxy:  指代我们所代理的那个真实对象
    				//method:  指代的是我们所要调用真实对象的某个方法的Method对象
    				//args:  指代的是调用真实对象某个方法时接受的参数
    				public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    					if("close".equals(method.getName())){
    						return pool.add(connection);
    					}else {
    						return method.invoke(connection, args);
    					}
    				}
    			});
    		}else {
    			throw new RuntimeException("系统错误!!");
    		}
    	}
    
    	@Override
    	public Connection getConnection(String username, String password) throws SQLException {
    		// TODO Auto-generated method stub
    		return null;
    	}
    public static void main(String[] args) throws SQLException {
    	LiYuDataBase db = new LiYuDataBase();
    	Connection connection = db.getConnection();
    	connection.close();
    }
    }
    

     

  2. 使用Java包裹的方式事项DataSource
package com.poweruniverse.app.job;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class LiYuDataBaseT implements DataSource{
	private static LinkedList<Connection> pool=new LinkedList<Connection>();
	private static final String driver="oracle.jdbc.OracleDriver";
	private static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	private static final String user="knowledge";
	private static final String password="knowledge";
	
	//采用包裹的方式实现数据源
	public LiYuDataBaseT() {
		for(int i=0;i<100;i++){
			try {
				Connection connection = DriverManager.getConnection(url);
				pool.add(connection);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@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 int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

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

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

	@Override
	public Connection getConnection() throws SQLException {
		return pool.size()>0?pool.removeFirst():null;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * @Title: close
	 * @Description: TODO(<将连接添加到pool中等待下次再用>)
	 */
	public void close(Connection con){
		pool.addLast(con);
	}

}

 查看博客:https://www.jb51.net/article/113761.htm

                  https://blog.csdn.net/ju_362204801/article/details/78886102

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值