dbutils的update方法与query方法的内部实现

首先假设有一个连接池 comboPooledDataSource

Class Dbutils
{   private DataSource dataSoure;

    public Dbutils(DataSource dataSource)
    {
        this.dataSource=dataSource;
    }
    private void InitParams(PreparedStatement preparedStatement,Object[] params) throws SQLException
    {
        for(int i=0;i<params.length;i++)
        {
            preparedStatement.setObject(i+1, params[i]);
        }
    }
    public int update(String sql,Object[] params)
    {
        Connection con = null;
        PreparedStatement pre = null;
        try{
            con=dataSource.getConnection();
            pre=con.preparedStatement();
            InitParams(pre,params);
            return preparedStatement.executeUpdate();
        }
        catch(Exception e) {
            // TODO: handle exception
        }
        finally
        {
                try {
                    if(preparedStatement!=null)preparedStatement.close();
                    if(connection!=null) connection.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return -1;
    }


    public <T> T querry(String sql,RsHandler<T> rh,Object[] params)
    {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet rs=null;
        try{
            con=dataSource.getConnection();
            pre=con.preparedStatement();
            InitParams(pre,params);
            rs=pre.executeQuery();
            if(rs.next()) return rh.handler(rs);
        }
        catch(Exception e) {
            // TODO: handle exception
        }
        finally
        {
                try {
                    if(rs!=null) rs.closer();
                    if(preparedStatement!=null)preparedStatement.close();
                    if(connection!=null) connection.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return null;
    }

interface RsHandler<T>
    {
        public T Handle(ResultSet resultSet) throws SQLException;
    }
}

/*
*   调用query方法
*/

public void fun1() throws SQLException
    {
        Dbutils dbutils=new Dbutils(comboPooledDataSource);

        String sql="select * from stu where sid=?";

        Object[] params={1002};

        RsHandler<Stu> rh = new RsHandler<Stu>(){
        public Stu Handle(ResultSet resultSet) throws SQLException
        {
            Stu stu  = new Stu();
            //从resultSet里获取值
            ..............
            return stu;
        };
        Stu stu = dbutils.querry(sql,rh,params)

        System.out.println(stu);
}

在真正的dbutils其实有更简单的实现,就是调用BeanHandler这个实现类

public void fun2() throws SQLException
{
    QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());

    String sql="select * from stu  where sid = ?";

    Object[] param={1002};

    Stu stu=queryRunner.query(sql, new BeanHandler<Stu>(Stu.class), param);

    System.out.println(stu);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值