当前位置:   article > 正文

Apache——DBUtils

Apache——DBUtils

韩顺平 零基础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);
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/431655
推荐阅读
相关标签
  

闽ICP备14008679号