DBUtils练习,增删改查,ResultSetHandler的9个结果处理器


/**
 * DBUtils练习,增删改查,ResultSetHandler的9个结果处理器
 */
public class TestDBUtils {

    @Test
    public void testSelect() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());
        List query = queryRunner.query("select * from user where password=?",
                new BeanListHandler(User.class), 321);
        for (User u : query) {
            System.out.println(u);
        }
    }

    @Test
    public void testInsert() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());
        queryRunner.update("insert into user(username,password,email,birthday) values(?,?,?,?)",
                "老张", "123", "[email protected]", "2013-3-2");
    }

    @Test
    public void testUpdate() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());
        queryRunner.update("update user set username=?,password=? where id=?",
                "王婉婉", "321", 2);
    }

    @Test
    public void testDelete() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());
        queryRunner.update("delete from user where id=?",
                2);
    }

    @Test
    public void testBatch() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());
        Object[][] objects = new Object[10][4];
        for (int i = 0; i < objects.length; i++) {
            objects[i][0] = "程啸" + i;
            objects[i][1] = "aaa" + i;
            objects[i][2] = i + "@q.c";
            objects[i][3] = "2017-02-" + i;
        }
        queryRunner.batch("insert into user(username,password,email,birthday) values(?,?,?,?)", objects);
    }

    @Test//ArrayHandler适合取1条数据,把这条数据的每列值封装到一个数组中
    public void test1() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        Object[] query = qr.query("select * from user where id=?", new ArrayHandler(), 13);
        for (Object o : query)
            System.out.println(o);
    }

    @Test//ArrayListHandler适合取多条数据,把这条数据的每列值封装到一个数组中,把数组封装到List中
    public void test2() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        List query = qr.query("select * from user", new ArrayListHandler());
        for (Object[] o : query) {
            for (Object o1 : o)
                System.out.print(o1);
            System.out.println();
        }
    }

    @Test//ColumnListHandler取某一列的数据,封装到List中
    public void test3() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        List query = qr.query("select username,password from user", new ColumnListHandler(2));
        for (Object u : query) {
            System.out.println(u);
        }
    }

    @Test//KeyedHandler:取多条数据,每一条记录封装到一个Map中,再把这个Map封装到另一个Map中,key为指定的字段值
    public void test4() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        Map> query = qr.query("select id,username,password from user where username=?",
                new KeyedHandler(1), "程啸2");
        for (Map.Entry> entry : query.entrySet()) {
            System.out.println(entry.getKey());
            for (Map.Entry entry1 : entry.getValue().entrySet()) {
                System.out.println(entry1.getKey() + "-->" + entry1.getValue());
            }
        }
    }

    @Test//MapHandler适合取1条数据,把当前记录的列名和列值放到一个Map中
    public void test5() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        Map query = qr.query("select * from user where id=?", new MapHandler(), 22);
        for (Map.Entry entry : query.entrySet())
            System.out.println(entry.getKey() + ":" + entry.getValue());
    }

    @Test//MapListHandler:取多条数据,每一条记录封装到一个Map中,再把这个Map封装到List中
    public void test6() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        List> query = qr.query("select id,username,password from user where username=?",
                new MapListHandler(), "程啸2");
        for (Map map : query) {
            for (Map.Entry entry : map.entrySet()) {
                System.out.println(entry.getKey() + "-->" + entry.getValue());
            }
        }
    }

    @Test//ScalarHandler:适合取单行单列数据
    public void test7() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        Object query = qr.query("select count(*) from user where username=?",
                new ScalarHandler(1), "程啸2");
        System.out.println(query.getClass().getName());
    }

    @Test//BeanHandler:适合取单条数据,并返回一个对象
    public void test8() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        Object query = qr.query("select * from user where username=?",
                new BeanHandler(User.class), "程啸2");
        System.out.println(query);
    }

    @Test//BeanListHandler:适合取多行数据,并返回对象List
    public void test9() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        List list = qr.query("select * from user where username=?",
                new BeanListHandler(User.class), "程啸2");
        for (User u:list) {
            System.out.println(u);
        }
    }
} 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(JavaEE实战)