码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-09-19 14:58:33      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:fss   vhd   sls   epp   adl   asp   ceo   spec   cfs   

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

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

 

 

 

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

技术分享

 

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

标签:fss   vhd   sls   epp   adl   asp   ceo   spec   cfs   

原文地址:http://www.cnblogs.com/qlqwjy/p/7551032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!