如何使用封装好的DBUtils工具类
发布日期:2022-02-01 14:28:18 浏览次数:36 分类:技术文章

本文共 6976 字,大约阅读时间需要 23 分钟。

1.1 DBUtils简介

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

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
queryAll() { try { QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); List
list = qr.query("select * from emp", new BeanListHandler
(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.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
getColumName() { QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); try { return qr.query("select * from emp",new ColumnListHandler
("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
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 + '}'; }}

转载地址:https://blog.csdn.net/shunshizhen120412/article/details/100149999 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:自定义数据库工具类,以及三种常用的连接池DBCP、C3p0、Druid
下一篇:es6中 Set属性方法,以及遍历

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月16日 01时05分30秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

java buffer nio_Java NIO之Buffer(缓冲区)入门 2019-04-21
android java加密_android 和java平台通用的AES加密解密 2019-04-21
java导出类_java导出excel工具类 2019-04-21
java学习手册下载_Java学习手册 2019-04-21
axios delete有请求体吗_关于axios请求——delete方法 2019-04-21
java 自助更改密码 api_搭建ldap自助修改密码系统--Self Service Password 2019-04-21
php继承exten,stylus中文文档 » 继承(@extend) » 张鑫旭-鑫空间-鑫生活 2019-04-21
mysql函数大全 pdf,MySQL函数大全 2019-04-21
php 常用文件系统函数,php 文件系统函数整理介绍 2019-04-21
android pm.java,java – AM / PM的Android DateFormat因设备而异 2019-04-21
oracle存储过程调用sql文件,oracle - 在SQL Developer中运行存储过程? 2019-04-21
oracle同时报604和12507,V$SES_OPTIMIZER_ENV 查不到刚修改的隐含参数, 2019-04-21
zblog的php更换域名,zblogphp更换域名后,原zblog里使用了固定域名,登录不进去怎么办... 2019-04-21
oracle dnfs 配置,Source of Oracle参数解析(dnfs_batch_size) - django-\/\/ i K | 2019-04-21
oracle所需的环境,转:面对一个全新的oracle环境,首先应该了解什么? 2019-04-21
linux 小数四则运行,shell四则运算(整数及浮点数)的方法介绍 2019-04-21
linux系统分区后进入紧急模式,Linux系统的救援模式应用详解 2019-04-21
linux配置匿名ftp服务器,在Linux环境中使用vsftpd搭建ftp实现匿名上传详细配置 2019-04-21
linux创建硬盘分区lvm,LVM创建及分区调整、更换LVM硬盘 2019-04-21
FreeBSD可以安装Linux软件吗,在Linux服务器上面通过网络安装FreeBSD 2019-04-21