利用dbutils工具类库实现对数据库的增删查改操作

commons-dbutils 是Apache组织提供的一个开源的JDBC工具类库,封装了针对于数据库的增删查改的操作。

package dbutils;

import bean.stuinfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import org.junit.Test;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * commons-dbutils 是Apache组织提供的一个开源的JDBC工具类库,封装了针对于数据库的增删查改的操作
 * @author LTH
 * @create 2021/2/2 - 10:51
 */
public class QueryRunnerTest {
    //测试插入(利用dbutils包里的update操作)
    @Test
    public void testInsertByDruid() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "insert into stuinfo(id, name, birthday) values(?,?,?)";
            int insertCount = runner.update(connection, sql, 10, "Druid", "2021-2-2");
            System.out.println("添加了" + insertCount + "条记录");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    //测试查询

    /*
     * BeanHandler:是ResultSetHandler接口的实现类,用于封装表中的一条记录
     */
    @Test
    public void testQueryByBeanHandler(){
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select id, name, birthday from stuinfo where id = ?";
            BeanHandler<stuinfo> handler = new BeanHandler<>(stuinfo.class);
            stuinfo si = runner.query(connection, sql, handler, 10);
            System.out.println(si);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }
    /*
     * BeanListHandler:是ResultSetHandler接口的实现类,用于封装表中多条记录构成的集合
     */
    @Test
    public void testQueryByBeanListHandler() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select id, name, birthday from stuinfo where id < ?";
            BeanListHandler<stuinfo> handler = new BeanListHandler<>(stuinfo.class);

            List<stuinfo> list = runner.query(connection, sql, handler, 10);
            list.forEach(System.out::println);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    /*
     * MapHandler:是ResultSetHandler接口的实现类,对应表中的一条记录,
     * 将字段及相应字段的值作为map中的key和value
     */
    @Test
    public void testQueryByMapHandler() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select id, name, birthday from stuinfo where id = ?";
            MapHandler handler = new MapHandler();

            Map<String, Object> map = runner.query(connection, sql, handler, 10);
            System.out.println(map);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    /*
     * MapListHandler:是ResultSetHandler接口的实现类,对应表中的多条记录,
     * 将字段及相应字段的值作为map中的key和value,将这些map添加到list中
     */
    @Test
    public void testQueryByMapListHandler() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select id, name, birthday from stuinfo where id < ?";
            MapListHandler handler = new MapListHandler();

            List<Map<String, Object>> list = runner.query(connection, sql, handler, 10);
            list.forEach(System.out::println);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    /*
     * ScalarHandler:用于查询特殊值
     */
    @Test
    public void testQueryByScalarHandler() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select count(*) from stuinfo";
            ScalarHandler handler = new ScalarHandler();

            Long count = (Long) runner.query(connection, sql, handler);
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    @Test
    public void testQueryByScalarHandler1() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select max(birthday) from stuinfo";
            ScalarHandler handler = new ScalarHandler();

            Date date = (Date) runner.query(connection, sql, handler);
            System.out.println(date);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }

    /*
     *自定义ResultSetHandler
     */
    @Test
    public void testQueryByResultSetHandler() {
        Connection connection = null;
        try {
            QueryRunner runner = new QueryRunner();
            connection = JDBCUtils.getConnectionByDruid();
            String sql = "select id, name, birthday from stuinfo where id = ?";
            ResultSetHandler<stuinfo> handler = new ResultSetHandler<stuinfo>() {
                @Override
                public stuinfo handle(ResultSet resultSet) throws SQLException {
//                    return new stuinfo(11, "成龙" , new Date(23423242423232L));
                    if(resultSet.next()){
                        int id = resultSet.getInt("id");
                        String name = resultSet.getString("name");
                        Date birthday = resultSet.getDate("birthday");
                        stuinfo si = new stuinfo(id, name, birthday);
                        return si;
                    }
                    return null;
                }
            };
            stuinfo si = runner.query(connection, sql, handler, 10);
            System.out.println(si);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.clossResource(connection, null);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值