7 JDBC Druid连接池的使用

1.工具类
(1)根据配置和工厂得到DataSource对象

 //先获取配置
            Properties properties = new Properties();
            //获取路径
            InputStream resourceAsStream = JDBC_util_2.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(resourceAsStream);
            //获取连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);

(2)工具应该包含的方法

 //获取连接对象
    public static Connection getCon() throws SQLException {
        return dataSource.getConnection();
    }
    //获取池子对象
    public static DataSource  getDataSource(){
        return  dataSource;
    }
    //关闭链接对象,实际是归还给池子
    public static void close(ResultSet rs , Statement stmt, Connection conn){


        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//归还连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement statement,Connection connection){
        close(null,statement,connection);
    }



}

3.完整工具类代码

public class JDBC_util_2 {
    //定义DataSouce
    private  static DataSource dataSource;
    //通过druid得到连接池
    static {

        try {
            //先获取配置
            Properties properties = new Properties();
            //获取路径
            InputStream resourceAsStream = JDBC_util_2.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(resourceAsStream);
            //获取连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接对象
    public static Connection getCon() throws SQLException {
        return dataSource.getConnection();
    }
    //获取池子对象
    public static DataSource  getDataSource(){
        return  dataSource;
    }
    //关闭链接对象,实际是归还给池子
    public static void close(ResultSet rs , Statement stmt, Connection conn){


        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//归还连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement statement,Connection connection){
        close(null,statement,connection);
    }



}

4,测试代码

public class Damo6 {
    private static PreparedStatement preparedStatement;
    private  static Connection con;
    private static ResultSet resultSet;

    public static void main(String[] args) {
        try {
           con = JDBC_util_2.getCon();
            String sql="select * from user1 where name=?";
            //使用准备状态解决注入问题
            preparedStatement = con.prepareStatement(sql);
            //给占位符对号入座
            preparedStatement.setString(1,"jiang");
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                String password = resultSet.getString("password");
                System.out.println("密码是   "+password);
            }



        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC_util_2.close(resultSet,preparedStatement,con);
        }
        }
    }


5.测试结果

七月 12, 2020 6:23:14 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
密码是   123

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值