韩顺平 零基础30天学会Java
一、基本介绍
1、commons-dbutils是 Apache 组织提供的一个开源JDBC工具类库,它是对JDBC的封装,使用dbutils能极大简化jdbc编码的工作量
二、DbUtils类
1、QueryRunner类:该类封装了SQL的执行,是线程安全的。可以实现增、删、改、查、批处理
2、使用QueryRunner类实现查询
3、ResultSetHandler接口:改接口用于处理java.sql.ResultSet,将数据按照要求转换成另一种形式
三、使用DBUtils+数据库连接池(Druid)方式完成对表的操作
-
actor表
public class DBUtils_Use {
//测试查询到多行结果
@Test
public void testQueryMany() throws Exception {
//1、得到连接
Connection connection = Druid.getConnection();
//使用DBUtils 类和接口,先引入DBUtils相关的jar,加入到本Project
//3、创建QueryRunner
QueryRunner queryRunner = new QueryRunner();
//4、就可以执行相关的方法返回ArrayList 返回结果集
// (1)query 方法就是执行sql语句,得到resultSet----封装到--->ArrayList集合中
//(2)返回集合
//(3)connection:连接
// (4)sql:执行的sql语句
// (5)new BeanListHandler<>(Actor.class):在将resultset-》actor对象-》封装到ArrayList
//底层使用反射机制,去获取Actor类的属性然后进行封装
// (6)1 就是给sql语句中的?赋值,可以有多个值,因为是可变参数
//(7)底层的到的resultSet,会在query 关闭,之后不用再关闭
String select = "select * from actor";
List<Actor> list = queryRunner.query(connection, select, new BeanListHandler<>(Actor.class));
System.out.println("输出集合信息");
for (Actor actor : list) {
System.out.println(actor);
}//释放连接 Druid.close(null, null, connection); } //测试查询到单行结果 @Test public void testSingle() throws Exception { //1、得到连接 Connection connection = Druid.getConnection(); //2、创建QueryRunner QueryRunner queryRunner = new QueryRunner(); //3、sql语句 String select = "select * from actor where id=?"; //因为知道返回的是单行记录,因此使用 BeanHandler Actor actor = queryRunner.query(connection, select, new BeanHandler<>(Actor.class), 3); System.out.println(actor); //释放资源 Druid.close(null, null, connection); } //演示查询单行单列的情况 @Test public void testScalar() throws Exception { //1、得到连接 Connection connection = Druid.getConnection(); //2、创queryRunner QueryRunner queryRunner = new QueryRunner(); //3、组织sql String select = "select name from actor where id=?"; //执行 Object query = queryRunner.query(connection, select, new ScalarHandler(), 3); System.out.println(query); //释放资源 Druid.close(null, null, connection); } //更新内容 @Test public void update() throws Exception { //1、获得连接 Connection connection = Druid.getConnection(); //2、创建QueryRunner QueryRunner queryRunner = new QueryRunner(); //3、组织sql语句 String update = "UPDATE actor SET `name`=? WHERE id=?"; //4、执行 int updateRows = queryRunner.update(connection, update, "新小龙女", 4); //5、释放资源 Druid.close(null, null, connection); } //删除内容 @Test public void delete() throws Exception { //1、获得连接 Connection connection = Druid.getConnection(); //2、创建QueryRunner QueryRunner queryRunner = new QueryRunner(); //3、组织sql String delete = "DELETE FROM actor WHERE id=3"; //4、执行 int deleteRows = queryRunner.update(connection, delete); System.out.println(deleteRows > 0 ? "成功" : "失败"); //5、释放资源 Druid.close(null, null, connection); } //增加内容 @Test public void insert() throws Exception { //1、获得连接 Connection connection = Druid.getConnection(); //2、创建QueryRunner QueryRunner queryRunner = new QueryRunner(); //3、组织sql String insert = "INSERT INTO actor VALUES(null,?,?,?,?)"; //4、执行 int insertRows = queryRunner.update(connection, insert, "张三", "男", "1999-08-24", "119"); System.out.println(insertRows > 0 ? "成功" : "失败"); //5、释放资源 Druid.close(null, null, connection); }
}