DBUtils使用

DBUtils简介

  • Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。
  • DBUtils是java编程中的数据库操作实用工具,小巧简单实用
    1. 对于数据表的读操作,可以把结果转换成List,Array,Set等java集合,便于程序员操作。
    2. 对于数据表的写操作,也变得很简单(只需写sql语句)。

DBUtils包括主要类

  • DBUtils包括主要类

ResultSetHandler接口:转换类型接口

  • ArrayHandler类:实现类,把记录转化成数组
  • ArrayListHandler类:把记录转化成数组,并放入集合中
  • ColumnListHandler类:取某一列的数据。封装到List中。
  • ScalarHandler类:适合获取一行一列数据。(经常使用)
  • BeanHandler类:实现类,把记录转成对象。(经常使用)
  • BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象(经常使用)

QueryRunner类:执行SQL语句的类

DBUtils使用步骤

导入jar包

  • (1)commons-dbutils-1.6.jar
  • (2)druid-1.1.5.jar
  • (3)mysql驱动包

database.properties配置文件

 #连接设置
 driverClassName=com.mysql.jdbc.Driver
 url=jdbc:mysql://localhost:3306/myschool?useSSL=true
 username=root
 password=root
 #<!-- 初始化连接 -->
 initialSize=10
 #最大连接数量
 maxActive=50
 #<!-- 最小空闲连接 -->
 minIdle=5
 #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
 maxWait=5000

DruidUtils.java工具类

public class DataSourceUtils {
   //使用Druid连接池
   private static DruidDataSource dataSource;
   static{
       try {
           Properties properties = new Properties();
           InputStream resourceAsStream = DataSourceUtils.class.getClassLoader().getResourceAsStream("database.properties");
           properties.load(resourceAsStream);
           resourceAsStream.close();
           dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   //返回DataSource数据源接口
   public static DataSource getDataSource() {
       return dataSource;
   }
}
  • 使用工具代码示例:(省略实体类和接口的代码)
public class StuDaoImpl implements StuDao {
   @Override
   public List<Stu> findStu() {//查询所有数据,返回集合
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           return qr.query("select * from stu", new BeanListHandler<Stu>(Stu.class));
       } catch (SQLException e) {
           e.printStackTrace();
           throw new RuntimeException("查询失败", e);
       }
   }

   @Override
   public Stu findOneStu(Integer sid) {//查找指定数据,返回Stu对象
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           return qr.query("select * from stu where sid = ?", new BeanHandler<Stu>(Stu.class), sid);
       } catch (SQLException e) {
           e.printStackTrace();
           throw new RuntimeException("查询" + sid + "失败", e);
       }
   }

   @Override
   public void addStu(Stu stu) {//添加数据
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       Object[] pasts = {stu.getSid(), stu.getSname(), stu.getGender(), stu.getAge()};
       try {
           qr.update("insert into stu values(?,?,?,?);", pasts);
       } catch (SQLException e) {
           e.printStackTrace();
           throw new RuntimeException("插入失败", e);
       }
   }

   @Override
   public void updateStu(Stu stu) {//修改数据
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       Object[] pasts = {stu.getSname(), stu.getGender(), stu.getAge(), stu.getSid()};
       try {
           qr.update("update stu set sname=?,gender=?,age=? where sid=?", pasts);
       } catch (SQLException e) {
           e.printStackTrace();
           throw new RuntimeException("修改失败", e);
       }

   }

   @Override
   public void deleteStu(Integer sid) {//删除数据
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           qr.update("delete from stu where sid = ?", sid);
       } catch (SQLException e) {
           e.printStackTrace();
           throw new RuntimeException("删除失败", e);
       }
   }

   @Override
   public long getCount() {//使用ScalarHandler获取表中共几条数据
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           Long query = qr.query("select count(*) from stu", new ScalarHandler<>());
           return query;
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return -1;
   }

   @Override
   public Object[] findArray(String gender) {//查询指定数据,返回数组
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           return qr.query("select * from stu where gender=?", new ArrayHandler(), gender);
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return null;
   }

   @Override
   public List<Object[]> findArrayListStu() {//查询所有数据,返回集合,但是数据存储在数组中,数组存储在集合中
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           return qr.query("select * from stu", new ArrayListHandler());
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return null;
   }

   @Override
   public List<Object> findListStu() {//获取表中某有一个数据,返回集合
       QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
       try {
           return qr.query("select * from stu", new ColumnListHandler<>("sname"));
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return null;
   }
}


转载请注明:Memory的博客 » 点击阅读原文

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦