首页 > 数据库 >数据库连接池-druid基本使用以及他的工具类

数据库连接池-druid基本使用以及他的工具类

时间:2022-10-22 16:57:51浏览次数:37  
标签:throwables 数据库 druid 连接池 close null public conn

数据库连接池-druid基本使用

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

  步骤:

    1.导入jar包:druid-1.0.9.jar

    2.定义配置文件:

      是properties形式的

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

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

    4.获取连接:getConnection 

/**
 * druid演示
 */
public class DruidDemo {
    public static void main(String[] args) throws Exception {
        //1、导入jar包
        //2、定义配置文件
        //3、加载配置文件
        Properties pro = new Properties();
        InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //4、获取连接池对象
        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 {
    //1.定义成员变量
    private static DataSource ds;

    static {
        try {
            //1.加载配置文件
            Properties pro = new Properties();
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //2.获取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;
    }
}
复制代码

 

数据库连接池-druid工具类测试

完成添加操作,给account表添加一条数据

复制代码
/**
 * 使用工具类
 */
public class DruidDemo2 {
    public static void main(String[] args) {
        /**
         * 完成添加操作,给account表添加一条数据
         */
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //1、获取连接
            conn = JDBCUtils.getConnection();
            //2、定义sql
            String sql = "insert into account values (null ,?,?)";
            //3、获取pstmt对象
            pstmt = conn.prepareStatement(sql);
            //4、给?赋值
            pstmt.setString(1,"赵六");
            pstmt.setDouble(2,3000);
            //5、执行sql
            int count = pstmt.executeUpdate();
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            JDBCUtils.close(pstmt,conn);
        }
    }
}
复制代码

数据库数据:

 

标签:throwables,数据库,druid,连接池,close,null,public,conn
From: https://www.cnblogs.com/qihaokuan/p/16816402.html

相关文章