模仿QueryRunner的底层实现

本文介绍了一个名为MyQueryRunner的实用工具类,该类用于简化Java应用程序中的数据库操作。通过使用MyQueryRunner,开发者可以方便地执行SQL查询和更新操作,并通过自定义的结果集处理器将查询结果转换为特定类型的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.xiechengxu.dbutils;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Created by zhangqiang on 17/6/23.
 */
public class MyQueryRunner {

    private DataSource ds;

    public MyQueryRunner() {
    }

    public MyQueryRunner(DataSource ds){

        this.ds=ds;
    }



    public <T> T query(Connection con,String sql ,MyResultSetHandler<T> mrs, Object ...params) throws SQLException {

        PreparedStatement pst=con.prepareStatement(sql);

        ParameterMetaData pmd=pst.getParameterMetaData();

        int count=pmd.getParameterCount();


        for(int i=1;i<=count;i++){
            pst.setObject(i,params[i-1]);
        }


        ResultSet rs=pst.executeQuery();//得到结果集,要将结果集封装成用户想要的对象,但是,工具不知道用户需求

        return mrs.handle(rs);
    }


    public int update(Connection con,String sql , Object ...params) throws SQLException {

        PreparedStatement pst=con.prepareStatement(sql);

        ParameterMetaData pmd=pst.getParameterMetaData();

        int count=pmd.getParameterCount();


        for(int i=1;i<=count;i++){
            pst.setObject(i,params[i-1]);
        }

        int row= pst.executeUpdate();

        //关闭资源
        pst.close();

        return row;

    }




    public int update(String sql , Object ...params) throws SQLException {


        Connection con=ds.getConnection();

        PreparedStatement pst=con.prepareStatement(sql);

        ParameterMetaData pmd=pst.getParameterMetaData();

        int count=pmd.getParameterCount();


        for(int i=1;i<=count;i++){
            pst.setObject(i,params[i-1]);
        }

        int row= pst.executeUpdate();

        //关闭资源
        pst.close();
        con.close();
        return row;

    }

}


package cn.xiechengxu.dbutils.domain;

import cn.xiechengxu.dbutils.MyQueryRunner;
import cn.xiechengxu.dbutils.MyResultSetHandler;
import cn.xiechengxu.utils.DataSourceUtils;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by zhangqiang on 17/6/23.
 */
public class MyQueryRunnerTest {


    @Test
    public void updateTest() throws SQLException{

        String sql="delete from account where id=?";

        MyQueryRunner mqr=new MyQueryRunner();

        mqr.update(DataSourceUtils.getConnection(),sql,3);

    }


    @Test
    public void selectTest() throws SQLException {

        String sql="select * from account where id=?";

        MyQueryRunner mqr=new MyQueryRunner();

       Account a=mqr.query(DataSourceUtils.getConnection(), sql, new MyResultSetHandler<Account>() {
            @Override
            public Account handle(ResultSet rs) throws SQLException {
                Account a=null;

                if(rs.next()){
                    a=new Account();
                    a.setId(rs.getInt("id"));
                    a.setName(rs.getString("name"));
                    a.setMoney(rs.getDouble("money"));
                }

                return a;
            }
        }, 2);



       System.out.print(a);
    }
}

public interface MyResultSetHandler<T> {

    public T handle(ResultSet rs)throws SQLException;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值