数据库连接池-druid-基本使用、druid工具类、druid工具类测试

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

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

步骤:导入包  druid-1.1.10.jar

    定义配置文件:

      是properties形式

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

  加载配置文件 :Propreties

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

  获取连接:getConnection

 

导入jar包

 

 

 

编写properties

 

 

编写代码

 

 

据库连接池-druid工具类

 

public class JDBCUtils {

    /**
     * 定义成员变量,DataSource
     */
    private static DataSource ds;

    static{

        try {
            /**
             * 加载配置文件
             */
            Properties properties = new Properties();
            /**
             * 获取配置文件
             */
            properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            /**
             * 获取DataSource
             */
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 获取连接
     */
    public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }
    /**
     * 释放资源
     */
    public static void close(Statement stmt,Connection coon){
        close(null,stmt,coon);
    }
    public static void close(ResultSet rs, Statement stmt, Connection coon){
        //判断rs不为空就关流
        if(rs!=null){
            try {
                //抛出异常,关流
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //判断stme不为空就关流
        if(stmt!=null){
            try {
                //抛出异常,关流
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //判断coon不为空就关流
        if(coon!=null){
            try {
                //抛出异常,归还连接
                coon.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
     * 获取连接池方法
     */
    public static DataSource getDataSource(){
        return ds;
    }
}

 

 

 

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

 

 public static void main(String[] args) {
        /**
         * 完成添加操作,给account表添加一条记录
         */
        Connection conn = null;
        PreparedStatement pstmt=null;
        try {
            //1获取连接
            conn = JDBCUtils.getConnection();
            //定义sql
            String sql = "insert into account value(null,?,?)";
            //获取pstmt对象
            pstmt = conn.prepareStatement(sql);
            //给第一个?赋值
            pstmt.setString(1,"王五");
            //给第2个?赋值
            pstmt.setString(2,"1223123");        
            //执行sql
            int i = pstmt.executeUpdate();
            //输出
            System.out.println(i);

        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            JDBCUtils.close(pstmt,conn);
        }
    }

 

 

posted @ 2022-10-18 14:54  漁夫  阅读(99)  评论(0)    收藏  举报