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

本文共 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属性方法,以及遍历

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月13日 10时36分36秒