Apache DBUtils常用方法总结

首先我们的明白Apache DBUtils是干什么用的:它是dao层的一个帮助类,简化了dao层jdbc的开发,下面直接开始讲述实现步骤:

1.下载commons-dbutils-1.7.jar
其中包含了三个基本类:
DbUtils辅助,QueryRunner:增删改查. ResultSetHandler查询返回集合
还需要引入一种数据源的jar,和jdbc的jar。
下面是各自的常用方法
1.DbUtils常用方法:
在这里插入图片描述
2.QueryRunner类常用方法:
在这里插入图片描述
3.ResultSetHandler:
在这里插入图片描述

测试:

数据库的表:
在这里插入图片描述
Student类:

package Clss.apacheddbutil;

public class Student {
private int stuno;
private String stuname;
private int stuage;
private String gname;

public Student(int stuno, String stuname, int stuage, String gname) {
        this.stuno = stuno;
        this.stuname = stuname;
        this.stuage = stuage;
        this.gname = gname;
    }
    public Student() {

    }
    public int getStuno() {
        return stuno;
    }

    public void setStuno(int stuno) {
        this.stuno = stuno;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public int getStuage() {
        return stuage;
    }

    public void setStuage(int stuage) {
        this.stuage = stuage;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }
}

查询:

package Clss.apacheddbutil;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import util.DataSourceUtil;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;

public class query {
    //查询,自动提交事务
    //返回单行Object
    public static void testArrayHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        Object[] student = runner.query("select *from student where stuno >? and stuname like ?",new ArrayHandler(),new Object[]{10,"%z%"});//ArrayHandaler只取一行
        System.out.println(student.length);
        System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
    }//顺序默认是oracle 表的顺序
    //返回多行Object
    public static void testArrayListHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        List<Object[]> students = runner.query("select *from student where stuno >?",new ArrayListHandler(),5);//ArrayHandaler只取一行
        for(Object[] student:students){
            System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
        }//顺序默认是oracle表的顺序

    }


    //Student
    //查询单行数据(放入对象中)
    public static void testBeanHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        Student student = runner.query("select *from student where stuno >?",new BeanHandler<Student>(Student.class),10);//ArrayHandaler只取一行
        System.out.println(student.getStuname()+","+student.getStuno()+","+student.getStuage()+","+student.getGname());
    }//注意创建的Student类里面的参数必须和该表的属性一模一样  比如说必须是Student类里面必须定义stuno ,不能写成stu
    //输出可以根据自己的喜好  调整输出参数的位置


    //查询多行数据
    public static void testBeanListHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        List<Student> students = runner.query("select *from student where stuno >?",new BeanListHandler<Student>(Student.class),10);//ArrayHandaler只取一行
        for(Student student:students) {
            System.out.println(student.getStuname() + "," + student.getStuno() + "," + student.getStuage() + "," + student.getGname());
        }
    }

    //查询多行数据(放入map中)
    public static void testBeanMapHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());                        //注意Oracle默认数据类型是BigDecimal,不能用Integer
        Map<BigDecimal,Student> students = runner.query("select *from student where stuno >?",new BeanMapHandler<BigDecimal,Student>(Student.class,"stuno"),10);//ArrayHandaler只取一行
        Student stu = students.get(new BigDecimal(13));//坑!!!!!不能写13 必须转成BigDecimal类型
        System.out.println(stu.getStuname() + "," + stu.getStuno() + "," + stu.getStuage() + "," + stu.getGname());
    }


.......................................Map   ->返回的结果为前面有属性名
    //返回第一行数据
public static void testMapHandler() throws SQLException {
    QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
    Map<String,Object> student = runner.query("select *from student where stuno >?",new MapHandler(),656);
    System.out.println(student);
}
    //返回第多行数据
    public static void testMapListHandler() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        List<Map<String,Object>> students = runner.query("select *from student where stuno >?",new MapListHandler(),10);
        for(Map<String,Object> student:students)
        System.out.println(student);
    }

//多行加字段   给{}一个key值
public static void testKeyedHandler() throws SQLException {
    QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
    Map<String,Map<String,Object>> students = runner.query("select *from student where stuno >?",new KeyedHandler<String>("stuname"),10);
    System.out.println(students);
}

///................................................把结果集中的某一列  保存到List中
public static void testColumnListHandler() throws SQLException {
    QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
    List<String> students = runner.query("select *from student where stuno >?",new ColumnListHandler<String>("stuname"),10);
    System.out.println(students);
}
//ScalarHandler :单值结果
public static void testScalarHandler() throws SQLException {
    QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
    BigDecimal result = runner.query("select count(1) from student where stuno >?",new ScalarHandler<BigDecimal>(),10);
    System.out.println(result);//sql: select stuno from student where stu0= ?;  param:13
}
public static void main(String[] args) throws SQLException {
//        testArrayHandler();
           testArrayListHandler();
        //testBeanHandler();
//        testBeanListHandler();
        //testBeanMapHandler();
//        testMapHandler();
//        testMapListHandler();
//        testKeyedHandler();
//        testColumnListHandler();
        //testScalarHandler();
    }
}

增删改:

package Clss.apacheddbutil;

import org.apache.commons.dbutils.QueryRunner;
import util.DataSourceUtil;

import java.sql.SQLException;

public class UpdateDemo {
    //自动提交
    //增加
    public static void add() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        int count = runner.update("insert into student(stuno,stuname,stuage,gname) values(?,?,?,?)",new Object[]{50,"huhuilin",18,"sss"});
        System.out.println(count);//返回增删改的 条数
    }

    //删除
    public static void delete() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        int count = runner.update("delete from student where stuno = ?",999);
        System.out.println(count);//返回增删改的 条数
    }

    //修改
    public static void update() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
        int count = runner.update("update student set stuname=? where stuno=?",new Object[]{"wangyin",4});
        System.out.println(count);//返回增删改的 条数
    }

  public static void main(String[] args) throws SQLException {
//        add();
//        delete();
          update();
    }
}

注意,有些ojdbc.jar包和本项目不兼容!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Apache DbUtils 是一个开源的 Java 库,它提供了一组简单的 API,用于在 JDBC 驱动程序上执行常见的数据库操作。它旨在使数据库访问更容易,更快速,更简单。 以下是 Apache DbUtils 的一些常见用法: 1. 查询数据表 您可以使用 DbUtils 查询数据库中的数据表。以下是一个示例: ```java QueryRunner queryRunner = new QueryRunner(dataSource); List<User> users = queryRunner.query("SELECT * FROM users", new BeanListHandler<>(User.class)); ``` 此代码段使用 QueryRunner 类从数据库中选择所有用户。结果存储在 User 类的对象列表中。 2. 插入数据 您可以使用 DbUtils 向数据库插入新数据。以下是一个示例: ```java QueryRunner queryRunner = new QueryRunner(dataSource); queryRunner.update("INSERT INTO users (name, email) VALUES (?, ?)", "John Doe", "john.doe@example.com"); ``` 此代码段使用 QueryRunner 类将新用户插入名为“users”的数据表。 3. 更新数据 您可以使用 DbUtils 更新数据库中的数据。以下是一个示例: ```java QueryRunner queryRunner = new QueryRunner(dataSource); queryRunner.update("UPDATE users SET email = ? WHERE name = ?", "jane.doe@example.com", "Jane Doe"); ``` 此代码段使用 QueryRunner 类更新名为“Jane Doe”的用户的电子邮件地址。 4. 删除数据 您可以使用 DbUtils 从数据库中删除数据。以下是一个示例: ```java QueryRunner queryRunner = new QueryRunner(dataSource); queryRunner.update("DELETE FROM users WHERE name = ?", "John Doe"); ``` 此代码段使用 QueryRunner 类从数据表中删除名为“John Doe”的用户。 总结Apache DbUtils 是一个强大而易于使用的 Java 库,可帮助您轻松地执行常见的数据库操作。无论您是新手还是有经验的开发人员,都可以使用 DbUtils 来简化您的代码和提高生产力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘歌唱家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值