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();
    }

运行截图:

愿你心如花木,向阳而开

全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
xdm怎么说&nbsp;要被拷打了&nbsp;担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务