dbutils封装对象,单列,一行一列(用)

基本用法:查找并封装对象与对象集合

    public User findUserByNamePassword(String name,String password){
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user where name='"+name+"' and password = '"+password+"'";
        User user = null;
        try {
            user = runner.query(sql, new BeanHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }



    public List<User> findAllUser(){
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from user";
        List<User> userList = null;
        try {
            userList = runner.query(sql, new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

 

 

查 询出单行单列记录

//获得全部的用户条数,返回一个整数
    public int getTotalCount() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from user";
        Long query = (Long) runner.query(sql, new ScalarHandler());
        return query.intValue();
    }

 

 

 

 将一列的数据存为集合取出

 

posted @ 2017-09-19 11:48  QiaoZhi  阅读(553)  评论(0编辑  收藏  举报