实现javax.sql.DataSource接口

实现Connection getConnection()方法

定义一个静态的成员属性LinkedList类型作为连接池,在静态代码块中初始化5条数据库连接,添加到连接池中,在getConnection方法中,当获取连接的时候在连接池中remove掉一条连接就可以了

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; public class JDBCTest {
public static void main(String[] args) throws Exception {
//使用反射的方式
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接,导包的时候,注意要导java.sql下的,面向接口编程
MyPool pool=new MyPool();
Connection conn=pool.getConnection();
//获取传输器对象
Statement statement=conn.createStatement();
//获取结果集对象
ResultSet resultSet=statement.executeQuery("select * from user");
//遍历
while(resultSet.next()){
String username=resultSet.getString("username");
System.out.println(username);
}
//关闭资源
resultSet.close();
statement.close();
pool.resetConn(conn); }
}

我的连接池

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.List;
import java.util.logging.Logger; import javax.sql.DataSource; /**
* 手写连接池
*
* @author taoshihan
*
*/
public class MyPool implements DataSource {
// 连接池
public static List<Connection> pool = new LinkedList<Connection>();
// 初始化
static {
try {
Class.forName("com.mysql.jdbc.Driver");
for (int i = 0; i < 5; i++) {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/java", "root", "root");
pool.add(conn);
}
} catch (Exception e) {
}
} /**
* 获取连接
*/
@Override
public Connection getConnection() throws SQLException {
// 如果池中没有连接
if (pool.size() == 0) {
for (int i = 0; i < 5; i++) {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/java", "root", "root");
pool.add(conn);
}
}
//先进先出
Connection conn=pool.remove(0);
System.out.println("获取一个连接,池里还剩余"+pool.size());
return conn;
}
/**
* 重置连接
*/
public void resetConn(Connection conn){
try {
if(conn!=null && !conn.isClosed()){
pool.add(conn);
System.out.println("还回一个连接,池里还剩余"+pool.size());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Connection getConnection(String username, String password)
throws SQLException {
return null;
} @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;
} }

使用继承,装饰,动态代理改造一个类中的方法

继承的缺点:此时我们已经得到了Connection对象,因此无法通过继承改造这个对象

装饰的测试实现:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; public class JDBCTest {
public static void main(String[] args) throws Exception { //测试装饰模式
Animal dog=new BigDog(new Dog());
dog.eat();
dog.sound(); }
} /**
* 装饰模式测试
* @author taoshihan
*
*/
interface Animal{
public void eat();
public void sound();
}
class Dog implements Animal{ @Override
public void eat() {
System.out.println("吃");
} @Override
public void sound() {
System.out.println("汪");
}
}
//此时我想修改Dog类中的sound方法
class BigDog implements Animal{
private Dog dog;
public BigDog(Dog dog) {
this.dog=dog;
}
/**
* 这个方法调原来的
*/
@Override
public void eat() {
dog.eat();
}
/**
* 这个方法进行装饰
*/ @Override
public void sound() {
System.out.println("大叫");
} }

动态代理:

        //测试代理模式
final Dog dog=new Dog();
Animal proxy=(Animal) Proxy.newProxyInstance(Dog.class.getClassLoader(),Dog.class.getInterfaces() , new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("sound".equals(method.getName())){
System.out.println("大叫");
return null;
}else{
return method.invoke(dog, args);
}
}
});
proxy.eat();
proxy.sound();

[javaEE] 数据库连接池和动态代理的更多相关文章

  1. 【Java EE 学习 15】【自定义数据库连接池之动态代理的使用】

    一.动态代理的作用 使用动态代理可以拦截一个对象某个方法的执行,并执行自定义的方法,其本质是反射 优点:灵活 缺点:由于其本质是反射,所以执行速度相对要慢一些 二.数据库连接池设计思想 1.为什么要使 ...

  2. 使用Java中的动态代理实现数据库连接池

    2002 年 12 月 05 日 作者通过使用JAVA中的动态代理实现数据库连接池,使使用者可以以普通的jdbc连接的使用习惯来使用连接池. 数据库连接池在编写应用服务是经常需要用到的模块,太过频繁的 ...

  3. 使用 JAVA 中的动态代理实现数据库连接池

    数据库连接池在编写应用服务是经常需要用到的模块,太过频繁的连接数据库对服务性能来讲是一个瓶颈,使用缓冲池技术可以来消除这个瓶颈.我们可以在互联网上找到很多关于数据库连接池的源程序,但是都发现这样一个共 ...

  4. 关于利用动态代理手写数据库连接池的异常 java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.sql.Connection

    代码如下: final Connection conn=pool.remove(0); //利用动态代理改造close方法 Connection proxy= (Connection) Proxy.n ...

  5. junit,面向切面开发(动态代理),工厂设计模式,数据库连接池

    1.junit junit又叫单元测试,好处是能进行批量测试,而且如果方法出现了问题能立刻定位出出现问题的方法,还有一个好处是感官效果很好,如果方法都通过了则显示绿条,否则显示红条 TestCase. ...

  6. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  7. javaEE(12)_数据库连接池

    一.直接获取数据库连接和通过池获取示意图: 二.编写数据库连接池 1.实现DataSource接口,并实现连接池功能的步骤: •在DataSource构造函数中批量创建与数据库的连接,并把创建的连接加 ...

  8. 4.使用Redis+Flask维护动态代理池

    1.为什么使用代理池 许多⽹网站有专⻔门的反爬⾍虫措施,可能遇到封IP等问题. 互联⽹网上公开了了⼤大量量免费代理理,利利⽤用好资源. 通过定时的检测维护同样可以得到多个可⽤用代理理. 2.代理池的要 ...

  9. Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题

    Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...

随机推荐

  1. “全栈2019”Java第五十章:继承与构造方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  2. kali linux之Bdfproxy

    Bdfproxy(mitmproxy) 基于流量劫持动态注入shellcode(arp欺骗,dns欺骗,流氓ap) 步骤: 开启流量转发---------sysctl -w net.ipv4.ip_f ...

  3. 虚拟机安装CentOS,网络配置

    CentOS6和CentOS7,基础配置差不多. 安装CentOs6,过程:略: 安装完成后,系统默认启用动态ip,每次重启主机IP可能就会变化,搭配Xftp和Xshell工具会很难受,因此设置静态I ...

  4. UIImageView笔记

    image的伸缩变换 最好是image和imageview的size一致.如果不一致,最终的效果由imageView的contentMode和image的属性共同决定. 如果image没有cap in ...

  5. 关于在iOS应用中跳转到AppStore

    1.获取app 在AppStore上的网址 eg: NSString * appURLStr = @"https://itunes.apple.com/cn/app/shi-ke-zu-qi ...

  6. Python3之文本操作

    文件操作示例分析: 文件操作一般要经历三个步骤: 打开文件 操作文件 关闭文件 读取操作示例: >>>f = open('test.txt', 'r') # 打开文件test.txt ...

  7. 快速上手日期插件laydate

    1.laydate 这款插件有独立版的,也有作为layui模块的,两种使用方式差不多,就引入有区别而已 <!--这是作为模块使用--><!DOCTYPE html> <h ...

  8. leetcode-796-Rotate String

    题目描述: We are given two strings, A and B. A shift on A consists of taking string A and moving the lef ...

  9. Angular material mat-icon 资源参考_Toggle

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  10. 更改Android编译软件版本(make/gcc/bision)

    一.make版本 1.下载make的压缩包 ftp://ftp.gnu.org/gnu/make/ 2.解压,安装 cd make-x.x ./configuration sh build.sh su ...