JDBC 连接池封装工具

回顾:
CRUD-PreparedStatement预编译执行SQL
JDBC事务
开启事务 connction.setAutoCommit(false);
提交事务connection.commit();
回滚事务connection.rollback();

1、读取配置文件
    }
Properties类
InputStream inputStream=JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");

ResourceBundle类

package com.ww.jdbc_demo_01.util;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcUtils {
    //1、读取配置文件里面的属性值
    //1-1、声明读取的属性值要赋值的变量
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //1-2、静态代码块,读取配置文件;加载驱动;
    static{
        //1、读取resources/jdbc.properties配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        url=bundle.getString("url");
        username= bundle.getString("username");
        password=bundle.getString("password");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //2、获取连接
    public static Connection getConnection(){
        Connection connection= null;
        try {
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;

    //3、释放资源
    public static void closeAll(ResultSet resultSet, Statement statement, Connection connection){
        try {
            if(resultSet!=null){
                resultSet.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

2、连接池
2.1、连接池概述
2.1.1、为什么要去使用连接池

存放多个连接的容器。
我会频繁的使用连接,一个连接从创建到销毁都会消耗我们的资源。
同时创建多个连接,存入集合,使用获取集合中没有被使用的连接,使用完后就将连接归还给集合。
2.1.2、连接池原理
如果连接池中基础连接被获取完了,要么等待3S,看是否有连接归还;如果没有就只能新建连接。
自定义连接池,使用的是LinkedList集合来存放我们的连接。

3、自定义连接池
创建类,连接池类(MyDataSource.java)
定义一个集合对象(LinkedList.java)
初始化连接,5个、10个
获取连接
归还连接

package com.ww.jdbc_demo_01.util;

import java.sql.Connection;
import java.util.LinkedList;

//1、新建连接池类
public class MyDataSource {
    //2、创建连接池
   private LinkedList connectionPool=new LinkedList();

   //3、初实始化5个连接
   public MyDataSource(){
        for(int i=1;i<=5;i++){
            //创建5个连接
            Connection connection=JdbcUtils.getConnection();
            connectionPool.add(connection);
        }
   }
   //4、获取连接,还有初始化好的连接就获取,没有子就创建新的连接
    public Connection getConnection(){
        Connection connection=null;
       if(connectionPool.size()>0){
           connection =connectionPool.removeFirst();
       }else{
           connection=JdbcUtils.getConnection();
       }
      return connection;
    }
    //5、归还连接,就是将使用完的连接,在次存入到我们的连接池对象
    public void addBack(Connection connection){
       connectionPool.addLast(connection);
    }
}

连接池使用

@Test
public void selectAllUser() throws SQLException {
    //1、查询所有SQL语句
    String sql="select * from user";
    //2、获取连接
    MyDataSource myDataSource=new MyDataSource();//new 时已经初始化有5个连接
    Connection connection=myDataSource.getConnection();//获取连接

    //3、创建PreparedStatement对象
    PreparedStatement preparedStatement=connection.prepareStatement(sql);
    //4、替换替换符(无参数)
    //5、执行SQL,拿到结果集
    ResultSet resultSet=preparedStatement.executeQuery();
    //6、取出结果集,存入到实体类;实体类存入到集合
    List list=new ArrayList();
    while(resultSet.next()){
        User user=new User();
        user.setId(resultSet.getInt("id"));
        user.setUsername(resultSet.getString("username"));
        user.setPassword(resultSet.getString("password"));
        user.setNickname(resultSet.getString("nickname"));

        list.add(user);
    }
    //7、释放资源,归还连接给连接池
    JdbcUtils.closeAll(resultSet,preparedStatement,null);
    myDataSource.addBack(connection);
    //8、集合处理,(1)返回return (2)打印输出了
    for(User user:list){
        System.out.println(user.toString());
    }
}

你可能感兴趣的:(java,数据库,网络)