Apache DbUtils工具类初学

下载jar包:(Linux下载1,Windows下载2)
http://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi
在这里插入图片描述
Apache DbUtils跟我们学jdbc时自己写的DBUtil很相似,只不过功能更加强大,我们不需要自己写工具类了,使用Apache DbUtils可以大量节省时间精力。不难,只需要知道有哪些主要的类会用就ok

1.查询:

//以下方法全为ResultSetHandler的实现类
	public static void testDbUtilQuery(){
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSortceByC3P0());
        try {
            //1.用数组接收查询到数据的第一行
            System.out.println("1.用数组接收查询到数据的第一行");
            Object[] object = runner.query("select * from student where sno > ?",new ArrayHandler(),1);
            System.out.println(Arrays.toString(object));

            //2.用集合数组接收查询到的所有数据
            System.out.println("*****************************************");
            System.out.println("2.用集合数组接收查询到的所有数据");
            List<Object[]> objects = runner.query("select * from student where sno > ?",new ArrayListHandler(),1);
            for (Object[] objects1 : objects) {
                System.out.print(Arrays.toString(objects1));
            }

            //3.用对象数组接收查询到的数据的第一行
            System.out.println("*****************************************");
            System.out.println("3.用对象数组接收查询到的数据的第一行");
            Student student = runner.query("select * from student where sno > ?",new BeanHandler<Student>(Student.class),1);
            System.out.println(student);

            //4.用对象数组接收查询到的所有数据
            System.out.println("*****************************************");
            System.out.println("4.用对象数组接收查询到的所有数据");
            List<Student> students = runner.query("select * from student where sno > ?",new BeanListHandler<Student>(Student.class),1);
            System.out.println(students);

            //5.在4的基础上加个key(Oracle中默认的数据类型是BigDecimal)
            System.out.println("*****************************************");
            System.out.println("5.在4的基础上加个key");
            Map<BigDecimal,Student> studentMap = runner.query("select * from student where sno > ?",new BeanMapHandler<BigDecimal,Student>(Student.class,"sno"),1);
            System.out.println(studentMap);

            //6.在5的基础上再加个key
            System.out.println("*****************************************");
            System.out.println("6.在5的基础上加个key");
            Map<String,Map<String,Object>> studentMaped = runner.query("select * from student where sno > ?",new KeyedHandler<String>("sname"),1);
            System.out.println(studentMaped);

            //7.查询某一列
            System.out.println("*****************************************");
            System.out.println("7.查询某一列");
            List<String> studentColumn = runner.query("select * from student where sno > ?",new ColumnListHandler<String>("sname"),1);
            System.out.println(studentColumn);

            //8.查询行数
            System.out.println("*****************************************");
            System.out.println("8.查询行数");
            BigDecimal studentCount = runner.query("select count(1) from student where sno > ?",new ScalarHandler<BigDecimal>(),1);
            System.out.println(studentCount);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //增删改
    public static void testDbUtilUpdata() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSortceByC3P0());
        //删除
        System.out.println("删除:" + queryRunner.update("delete from student where sno = ?", new Object[]{12}));
        //插入
        System.out.println("插入:" + queryRunner.update("insert into student values(?,?,?,?)", new Object[]{12, "郭金", 56, "阿拉尔"}));
        //修改
        System.out.println("修改:" + queryRunner.update("update student set sname = ? where sno = ?", new Object[]{"郭金xiugai",12}));
    }

    public static void main(String[] args) throws SQLException {
        testDbUtilQuery();
        testDbUtilUpdata();
    }

运行截图:
在这里插入图片描述

愿你心如花木,向阳而开

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值