数据库连接池_druid_基本使用和数据库连接池_druid_工具类

数据库连接池_druid_基本使用

Druid:数据库连接池实现技术,由阿里巴巴提供的

  步骤:

    1.导入jar包 druid-1.0.9.jar

    2.定义配置文件:

      是properties形式的

      可以叫任意名称,可以放在任意目录下

    3.加载配置文件。properties

    4.获取数据库连接池对象:通过工厂来获取 DruidDataSourceFactory

    5.获取连接:getConnection

配置文件:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db2
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
/*
Druid演示
*/
public class DruidDemo {
public static void main(String[] args) throws Exception {
//导入jar包
//定义配置文件
//加载配置文件
Properties pro = new Properties();
InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//获取连接
Connection conn = ds.getConnection();
System.out.println(conn);
}
}

 

数据库连接池_druid_工具类

 定义工具类

  1.定义一个类 JDBCUtils

  2.提供静态代码块加载配置文件,初始化连接池对象

  3.提供方法

    1.获取连接方法:通过数据库连接池获取连接

    2.释放资源

    3.获取连接池的方法

/*
Druid连接池工具类
 */
public class JDBCUtils {
    //定义成员变量
    private static DataSource ds;
    static {
        Properties pro = new Properties();
        try {
            //加载配置文件
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
        获取连接
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    /*
        释放资源
     */
    public static void close(Statement stmt,Connection conn){
        /*if (stmt !=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn !=null){
            try {
                conn.close();   //归还连接
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }*/
        //简化代码
        close(null,stmt,conn);
    }
    public static void close(ResultSet rs, Statement stmt,Connection conn){
        if (rs !=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (stmt !=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn !=null){
            try {
                conn.close();   //归还连接
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /*
        获取连接池
     */
    public static DataSource getDataSource(){
        return ds;
    }
}

测试:

/*
使用新工具类
 */
public class DruidDemo2 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取连接
            conn = JDBCUtils.getConnection();
            //定义sql
            String sql = "insert into account values (null ,?,?)";
            //获取pstmt对象
            pstmt = conn.prepareStatement(sql);
            //给?赋值
            pstmt.setString(1,"王五");
            pstmt.setInt(2,3000);
            //执行sql
            int count = pstmt.executeUpdate();
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }
    }
}

 

 

 

posted @ 2022-07-28 09:05  魔光领域  阅读(54)  评论(0编辑  收藏  举报