• ##DBUtils工具类的正确使用(一)


    DBUtils工具类(一)

    2.1 概述

    ​   DBUtils 是java中数据库操作的使用工具 DBUtils封装了JDBC操作 简化了JDBC操作 可以少些代码

    ​ 2.2 三个核心功能

      ​ QueryRunner 中提供了sql语句操作的API

      ​ ResultSetHandler 接口 用于select 操作后 怎样封装结果集

      ​ Dbutils 工具类 关闭资源 事物的处理方法

    ​ 2.3 准备数据 product表

    CREATE TABLE product(
        pid INT PRIMARY KEY AUTO_INCREMENT,
        pname VARCHAR(20),
        price INT,
        category_cid VARCHAR(20)
    );
    INSERT INTO product(pname,price,category_cid)VALUES('华为',4999,'soo1'),
    ('小米',1234,'soo1'),
    ('劲霸男装',120,'soo2'),
    ('红蜻蜓',300,'soo2'),
    ('香奈儿',800,'soo3'),
    ('大宝',10,'soo3'),
    ('果冻',20,'soo4'),
    ('饼干',3,'soo4');
    SELECT*FROM product;

    ​ 2.4 QueryRunner---》 newQueryRunner(DataSource);

    ​   update(String sql, Object ...prams) DML增删改

      ​ query(String sql,ResultSetHandler,Object ...prams));

    2.5 apache公司的commons组件 ---》jdbc6部---》3步(jdbc)
    QueryRunner 中提供了sql语句操作的API 增删 改查
    update query

    ResultSetHandler 接口 用于select 操作后 接受查询的结果集

    Dbutils 工具类 关闭资源 事物的处理方法 释放资源
    2.6使用步骤
      1 创建QueryRuner 对象
      2 调用QueryRuner 对象的方法 update query
      3 处理结果

    下面我们来看代码:

    1.1增加数据

     @Test
        public void test01DbUtils() throws SQLException {
            //1  创建QueryRuner 对象
            QueryRunner qr = new QueryRunner();
            //2 调用QueryRuner 对象的方法  update query
            Connection conn = C3P0xmlUtils.getconnection();
            String sql="insert into product(pname,price,category_cid)values(?,?,?)";
            int row = qr.update(conn, sql, "花生", 20, "s004");
            System.out.println(row);
            DbUtils.closeQuietly(conn);
        }

    1.2更改数据

    第一种: 
    @Test
    public void test02DbUtils() throws SQLException { //1,创建QueryRunner对象 QueryRunner qr = new QueryRunner(); //2,调用queryrunner对象的方法 Connection conn = C3P0xmlUtils.getconnection(); String sql="update product set pname=?where pid=?"; int rwo = qr.update(conn,sql,"海尔",1); System.out.println(rwo); DbUtils.closeQuietly(conn); }
    第二种:
    @Test
    public void test03DbUtils() throws SQLException {
    QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDataSource());
    int row = qr.update("update product set pname=?where pid=?", "荣耀", 2);
    System.out.println(row);
    }

    1.3删除数据

    @Test
        public void test04DbUtils() throws SQLException {
            QueryRunner qr = new QueryRunner(new ComboPooledDataSource());
    //        int row = qr.update("delete from product where pid=?", 1);
            int row = qr.update("delete from product where pid in(?,?,?)",2, 3, 4);
            System.out.println(row);
        }
    重点:
    使用Dbutils工具包对数据库的表进行查询 使用结果集BeanHnadler

    * 使用方式:把查询的结果第一行数据取出来 存储到javabean对象中返回
    * 构造方法
    * BeanHandler(Class<T> type) 传递javabean的class文件对象 Product.class
    2,1查询  
    BeanHandler(Class<T> type)
    @Test
        public void test05DbUtils() throws SQLException {
            QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDataSource());
            Product o = qr.query("select*from product", new BeanHandler<>(Product.class));
            System.out.println(o);
        }

    2,2查询  

    使用Dbutils工具包对数据库的表进行查询   使用结果集BeanListHnadler
    使用方式把查询的多行结果存储到多个javabean中 -----》List集合
    
    
    @Test
        public void test06() throws SQLException {
            QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDataSource());
            List<Product> list = qr.query("select*from product", new BeanListHandler<>(Product.class));
            for(Product p:list){
                System.out.println(p);
            }
        }
    
    

    2,3查询

    使用Dbutils工具包对数据库的表进行查询   使用结果集ScalarHnadler  用于接受slq语句是单一返回的情况
    * 使用方式:
    * 1 聚合函数 sum avg count max min
    * 2 获得某一行的某一字段的值
    * select pname from product where pid = ?
    
    
    @Test
        public void test07() throws SQLException {
            QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDataSource());
    //        Object q = qr.query("select count(price)from product", new ScalarHandler());
            Object q = qr.query("select pname from product where pid=?", new ScalarHandler(),5);
            System.out.println(q);
        }
    
    

    2.4查询

    使用Dbutils工具包对数据库的表进行查询   使用结果集ColumnListHandler
    用于查询指定的列 数据储存到List集合
    
    
    @Test
        public void test08() throws SQLException {
            QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDataSource());
            List<Object> list = qr.query("select pid,pname from product", new ColumnListHandler());
            for(Object o:list){
                System.out.println(o);
            }
        }
  • 相关阅读:
    Androi“.NET研究”d 源代码结构 狼人:
    Android UI“.NET研究”之困 横跨四个屏幕的战争 狼人:
    Win7 配“.NET研究”置Android开发环境 狼人:
    微“.NET研究”软“重启”Windows Phone 7 设计的经过 狼人:
    iOS开发库的族“.NET研究”谱介绍 狼人:
    文件匹配常用Linux命令小结
    functionclassAS sin函数小球运动
    持久化框架数据持久化框架(JPA)及其具体实现
    系统工程师Python工程师基础班
    设置虚拟机虚拟机中fedora上网配置bridge连接方式(图解)
  • 原文地址:https://www.cnblogs.com/liurui-bk517/p/11019331.html
Copyright © 2020-2023  润新知