淘先锋技术网

首页 1 2 3 4 5 6 7
commons-dbutils是Apache组织提供的一个开源JDBC工具类库,封装了对于数据库的增删改查操作
 public void testInsert() throws SQLException {
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils2.getConnection3();
            String sql="insert into customers(name,email,birth)values(?,?,?)";
            int insertCount = runner.update(conn, sql, "蔡徐坤", "qq.com", "1994-03-12");
            System.out.println("添加了"+insertCount+"条记录");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeRessourse(conn,null);
        }
    }

    //测试查询
    /*
    BeanHander:是resultSetHandler接口的实现类,用于封装表中的记录
     */
    @Test
    public void testQuery() throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection3();
        String sql="select name from customers where id = ?";
        BeanHandler<Customer>handler=new BeanHandler<>(Customer.class);
        Customer customer = runner.query(conn, sql, handler, 23);
        System.out.println(customer);
        JDBCUtils.closeRessourse(conn,null);
    }
    @Test
    public void testQuery2() throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection3();
        String sql="select name,email from customers where id < ?";
        BeanListHandler<Customer>handler=new BeanListHandler<>(Customer.class);
        List<Customer> list = runner.query(conn, sql, handler, 23);
        list.forEach(System.out::println);
        JDBCUtils.closeRessourse(conn,null);
    }
    @Test
    public void testQuery3() throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection3();
        String sql="select name,email from customers where id = ?";
        MapHandler handler=new MapHandler();
        Map<String,Object> map=runner.query(conn,sql,handler,1);
        System.out.println(map);
        JDBCUtils.closeRessourse(conn,null);
    }
    @Test
    public void testQuery4() throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection3();
        String sql="select name,email from customers where id < ?";
        MapListHandler handler=new MapListHandler();
        List<Map<String,Object>> list = runner.query(conn, sql, handler, 23);
        list.forEach(System.out::println);
        JDBCUtils.closeRessourse(conn,null);
    }
    @Test
    public void testQuery5() throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils2.getConnection3();
        String sql="select count(*) from customers";
        ScalarHandler handler = new ScalarHandler();
        Long count = (Long) runner.query(conn, sql, handler);
        System.out.println(count);
        JDBCUtils.closeRessourse(conn,null);
    }

通过观察构造器可知道还需要传入类型 

ALL Classes下的为resultSetHandler接口的实现类,用于封装表中的记录,针对不同的情况返回不同的数据

自定义ResultSetHandler的实现类

   public void testQuery7(){
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils2.getConnection3();

            String sql = "select id,name,email,birth from customers where id = ?";
            ResultSetHandler<Customer> handler = new ResultSetHandler<Customer>(){

                @Override
                public Customer handle(ResultSet rs) throws SQLException {
//					System.out.println("handle");
//					return null;

//					return new Customer(12, "成龙", "[email protected]", new Date(234324234324L));

                    if(rs.next()){
                        int id = rs.getInt("id");
                        String name = rs.getString("name");
                        String email = rs.getString("email");
                        Date birth = rs.getDate("birth");
                        Customer customer = new Customer(id, name, email, birth);
                        return customer;
                    }
                    return null;

                }

            };
            Customer customer = runner.query(conn, sql, handler,23);
            System.out.println(customer);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUtils.closeRessourse(conn, null);

        }

    }