dbutils封装对象,单列,一行一列(用)
时间:2021-07-01 10:21:17
帮助过:12人阅读
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();
}
将一列的数据存为集合取出