实现自定义数据库连接池

流程分析

  • 实现JDBCUtils的工具类,优化CURD操作
  • 实现一个数据库连接池
  • 使用装饰者模式实现Connection接口
1、实现JDBCUtils工具类
 private static String driverName;
    private static String url;
    private static String username;
    private static String password;
    private static MyDataSource mds = new MyDataSource();

    static {
        try{
          Properties p = new Properties();
          p.load("src/JDBCUtils.properties");
          driverName = p.get("driverName");
          url = p.get("url"); 
          username = p.get("username");
          password = p.get("password");
      }catch(Exception e){
          e.printStackTrace();
      }
    }
    
    //加载驱动
    public static void loadDriver () {
        try{
            Class.forName(driverName);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //创建连接
    public static Connection getConnection() {
        Connection con = null;
        try{
            loadDriver();
            con = DriverManager.getConnection(url,username,password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return con;
    }
    
    //释放资源,有结果集和无结果集的重载
    public static void release (ResultSet rs,Statement stat,Connection con){
        if(rs!=null){
            try{
                rs.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(stat!=null){
            try{
                stat.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(con!=null){
            try{
                con.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void release (Statement stat,Connection con){
        if(stat!=null){
            try{
                stat.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(con!=null){
            try{
                con.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }  
}
2、实现一个数据库连接池
public class MyDataSourcePool implements DataSource {
    private List list = new ArrayList<>();
    //程序开始的时候,初始化几个连接,将连接存放到list中
    static {
        for(int i = 0; i < 3; i++){
            Connection con = JDBCUtils.getConnection();
            list.add(con);
        }
    }
    
    //从连接池中获取连接
    public Connection getConnection () throws SQLException {
        //如果连接数量不够了,就新增连接
        if(list.size() <= 0){
            for(int i = 0; i < 3; i++){
            Connection con = JDBCUtils.getConnection();
            list.add(con);
            }
        }
        Connection con = list.remove(0);
        MyConnection mycon = new MyConnection(con,list);
        return mycon;
       }
    }
3、利用装饰者模式实现Connection接口
  public class MyConnection implements Connection {
    private Connection con;
    public MyConnection (Connection con,List list){
        this.con = con;
    }
    //重写close方法
    public void close () throws SQLException {
        list.add(con);
    }
    //以下是所有接口要求实现方法,注意测试时要实现PreparedStatement
    //return con.preparedStatement();
    ...
}
4、测试类
public class TestDemo {
    public void demo () {
        Connection con = null;
        PrepareStatement ps = null;
        ResultSet rs = null;
        try{
            //从连接池获取连接
            con = new MyDataSourcePool().getConnection();
            //sname(varchar) sage(int) ssex(varchar)
            String sql = "select * form student";
            ps = con.preparedStatement(sql);
            rs = ps.excuteQuery();
            while(rs.next()){
                System.out.println("name:"+rs.getString("sname"));
                System.out.println("age:"+rs.getInt("sage"));
                System.out.println("name:"+rs.getString("snsex"));
                System.out.println("------------------------"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //释放资源
            JDBCUtils.release(rs,ps,con);
        }
    }
}

感兴趣的的话也可以尝试继承和代理模式实现>0__0<

你可能感兴趣的:(实现自定义数据库连接池)