2020-10-08

本文介绍了Apache Commons DbUtils工具库的使用,它简化了JDBC应用程序的开发,通过QueryRunner类和ResultSetHandler接口进行数据操作。示例展示了如何利用Druid数据源进行连接管理,以及如何执行SQL增删改查操作,如BeanHandler、BeanListHandler等处理结果集。此外,还提供了DruidUtils配置及Druid数据源的初始化方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1 DBUtils简介
Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。

DBUtils是java编程中的数据库操作实用工具,小巧简单实用,
1.对于数据表的读操作,可以把结果转换成List,Array,Set等java集合,便于程序员操作。
2.对于数据表的写操作,也变得很简单(只需写sql语句)。

1.2 DBUtils包括主要类
QueryRunner类:执行SQL语句的类
ResultSetHandler接口:转换类型接口(程序中下面的几种实现类)
​–ScalarHandler类:适合获取一行一列数据。(比如数据的条数,某列的平均值,总和等聚合函数)
​–BeanHandler类:实现类,把记录转成对象。(查询一条信息)
​–BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象(通常是查询多条信息)
​–ArrayHandler类:实现类,把记录转化成数组(将查询的单条数据转换成数组)
​–ArrayListHandler类:把记录转化成数组,并放入集合中(将查询到的多条数据转换成数组集合,每一条数据 存放在一个数组,再放到集合中)
​–ColumnListHandler类:取某一列的数据。封装到List中。

1.3 DBUtils工具类封装
项目需要的jar包和配置文件:
commons-dbutils-1.6.jar
druid-1.1.5.jar(这里用什么连接池就导入那个连接池的jar,配置文件也是一样)
DruidUtils.java工具类
druid.properties配置文件

代码实现:

//首先创建一个数据连接池工具类 一个方法返回DataSource 用于工具类的参数
public class DataSourceUtils {
private static DruidDataSource dataSource;
static {
try {
Properties properties=new Properties();
InputStream ins = DataSourceUtils.class.getClassLoader().getResourceAsStream(“druid.properties”);
properties.load(ins);
//初始化 dataSource
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
System.out.println(“初始化失败”);
}
}

//设计方法  返回数据库连接池dataSource
public static DataSource getDataSource(){
        return dataSource;
}

}

//利用工具类来进行数据操作

//dao接口
public interface EmpDao {
void insertEmp(Emp emp);
void delete(Integer empno);
int update(Emp emp);
List queryAll();
Emp queryByEmpno(Integer empno);
long getcount() throws SQLException;//获取数据库的数据条数
List getColumName(); //查询数据库中的name列 所有姓名
}

//实现类
public class EmpDaoImpl implements EmpDao {
@Override
public void insertEmp(Emp emp) {
try {
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
Object[] params={emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()};
qr.update(“insert into emp values(?,?,?,?,?,?,?,?)”,params);
System.out.println(“增加成功”);
} catch (SQLException e) {
e.printStackTrace();
}

}

@Override
public void delete(Integer empno) {
    try {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        qr.update("delete from emp where empno=?",empno);
        System.out.println("删除成功");
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

@Override
public int update(Emp emp) {
    try {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        Object[] params={emp.getEname(),emp.getJob(),emp.getEmpno()};
        System.out.println("修改成功");
        return qr.update("update emp set ename=?,job=? where empno=?",params);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return -1;
}

@Override
public List<Emp> queryAll() {
    try {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        List<Emp> list = qr.query("select * from emp", new BeanListHandler<Emp>(Emp.class));
        return list;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("查询失败",e);
    }
}

@Override
public Emp queryByEmpno(Integer empno) {
    try {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        Emp emp = qr.query("select * from emp where empno=?", new BeanHandler<Emp>(Emp.class),empno);
        return emp;
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("查询失败",e);
    }
}

@Override
public long getcount() throws SQLException {
    QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
    Long count = qr.query("select count(*) from emp", new ScalarHandler<>());
    return count;
}

@Override
public List<String> getColumName() {
    QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
    try {
        return qr.query("select * from emp",new ColumnListHandler<String>("ename"));
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("查询失败",e);
    }
}

}

//测试类
public class Test {
public static void main(String[] args) throws SQLException {
queryByempno1();
// queryAll();
// delete();
// insert();
EmpDaoImpl empDao=new EmpDaoImpl();
List columName = empDao.getColumName();
for (String s : columName) {
System.out.println(s);
}

    EmpDaoImpl impl=new EmpDaoImpl();
    long getcount = impl.getcount();
    System.out.print("数据条数:"+getcount);

}
public static void insert(){
    Emp emp=new Emp(6666,"二哈","看门",null,new Date(),new BigDecimal(3000),new BigDecimal(300),20);
    EmpDaoImpl impl=new EmpDaoImpl();
    impl.insertEmp(emp);
}
public static void delete(){
    EmpDaoImpl impl=new EmpDaoImpl();
    impl.delete(6666);
}
public static void update(){
    Emp emp=new Emp(6666,"大黄","玩",null,new Date(),new BigDecimal(3000),new BigDecimal(300),20);
    EmpDaoImpl impl=new EmpDaoImpl();
    impl.update(emp);
}
public static void queryAll(){
    EmpDaoImpl impl=new EmpDaoImpl();
    List<Emp> list = impl.queryAll();
    for (Emp emp : list) {
        System.out.println(emp);
    }
}
public static void queryByempno1(){
    EmpDaoImpl impl=new EmpDaoImpl();
    Scanner sc=new Scanner(System.in);
    System.out.println("输入查询的号");
    int empno = sc.nextInt();
    Emp emp = impl.queryByEmpno(empno);
    System.out.println(emp);
}

}

//实体类
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Integer deptno;

public Emp() {
}

public Integer getEmpno() {
    return empno;
}

public void setEmpno(Integer empno) {
    this.empno = empno;
}

public String getEname() {
    return ename;
}

public void setEname(String ename) {
    this.ename = ename;
}

public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}

public Integer getMgr() {
    return mgr;
}

public void setMgr(Integer mgr) {
    this.mgr = mgr;
}

public Date getHiredate() {
    return hiredate;
}

public void setHiredate(Date hiredate) {
    this.hiredate = hiredate;
}

public BigDecimal getSal() {
    return sal;
}

public void setSal(BigDecimal sal) {
    this.sal = sal;
}

public BigDecimal getComm() {
    return comm;
}

public void setComm(BigDecimal comm) {
    this.comm = comm;
}

public Integer getDeptno() {
    return deptno;
}

public void setDeptno(Integer deptno) {
    this.deptno = deptno;
}

public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, BigDecimal sal, BigDecimal comm, Integer deptno) {
    this.empno = empno;
    this.ename = ename;
    this.job = job;
    this.mgr = mgr;
    this.hiredate = hiredate;
    this.sal = sal;
    this.comm = comm;
    this.deptno = deptno;
}

@Override
public String toString() {
    return "Emp{" +
            "empno=" + empno +
            ", ename='" + ename + '\'' +
            ", job='" + job + '\'' +
            ", mgr=" + mgr +
            ", hiredate=" + hiredate +
            ", sal=" + sal +
            ", comm=" + comm +
            ", deptno=" + deptno +
            '}';
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值