DButils
DBUtils
什么是DBUtils
DBUtils是一个实用小巧的工具,封装了JDBC.简化了JDBC的操作.
对于数据库的读操作,只需要知道sql语句即可.
DBUtils会将查询的结果集以指定的方式封装起来,比如List, javaBean等.同时支持数据源技术.使用时需要引入DButils的jar包:
点我获取:) 密码:f6l6
DBUtils的API
QueryRunner对象
-
1.对象描述
该对象用来提供执行增删改查的方法. -
2.方法
query() 查询方法
update() 增删改方法
ResultSetHandler对象(接口)
- 1.对象描述
用来对查询的结果集进行封装 - 2.该接口常用实现类
- ArrayHandler
适合结果集是一行数据,该实现类会将这行数据封装成一个数组对象,元素即为查询出来的字段值. - ArrayListHandler
适合结果集是多行数据,会将这些数据封装成一个List集合,该集合的元素是数组,数组即为每行数据 - ColumnListHandler
适合结果集是多行单列,会将这些数据封装到一个List集合 - KeyedHandler
适合取多条数据,会将每条数据封装成一个map集合,最后再把这些map集合封装成大map中 - MapHandler
适合结果集是一条数据,封装到map集合中 - MapListHandler
适合取多条数据,会将每条数据封装成一个map集合,最后再把这些map集合封装成List中 - ScalarHandler
适合取单行单列数据,比如聚合函数 - BeanHandler
适合取单行数据,将数据封装成对象 - BeanListHandler
适合取多行数据,将每行数据封装成一个对象,再把这些对象封装成一个List集合
- ArrayHandler
举个栗子BeanListHandler
public class Demo {
public static void main(String[] args) throws SQLException {
//执行查询
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner();
Connection connection = JDBCUtil.getConnection(); //此方法是自定义实现的参考连接池那篇随笔
//查询多行,并封装成对象,再放进List集合中
List<Employee> list = queryRunner.query(connection, "select * from emp", new BeanListHandler<Employee>(Employee.class));
for(Enployee e:list){
System.out.println(e);
}
}
}
栗子BeanHandler:
//执行查询
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
//查询一行,封装成对象
Employee emp = queryRunner.query(connection, "select * from emp where e_id = ?", new BeanHandler<Employee>(Employee.class), 1); //此处的1是where语句等号后面的?,做参数
栗子KeyedHandler:
//执行查询
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
Map<Object, Map<String, Object>> map = queryRunner.query(connection, "select * from emp", new KeyedHandler(2));
//遍历map集合
Set<Map.Entry<Object, Map<String, Object>>> set = map.entrySet();
for(Map.Entry<Object, Map<String, Object>> entry : set){
Object bigKey = entry.getKey();
Map<String, Object> smallMap = entry.getValue();
System.out.println(bigKey); //关羽
Set<String> smallSet = smallMap.keySet();
for(String smallKey : smallSet) {
Object value = smallMap.get(smallKey);
System.out.println(smallKey + ":" + value);
}
}
栗子ScalarHandler:
//执行查询
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
//聚合函数
long num = (long) queryRunner.query(connection, "select count(*) from emp", new ScalarHandler());
System.out.println(num);
栗子:
//执行查询
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
//增
Employee employee = new Employee();
employee.setE_id(18);
employee.setE_name("吕布");
employee.setE_salary(10000.0);
employee.setE_did(1);
employee.setHiretime(new Date());
employee.setE_job("staff");
Object[] param = {employee.getE_id(), employee.getE_name(), employee.getE_salary(), employee.getE_job(), employee.getE_did(), employee.getHiretime()};
int row = queryRunner.update(connection, "delete from emp where e_name = ?", employee.getE_name());
System.out.println(row);
JDBCUtil.close(connection);
频频回头的人注定走不了远路。