DbUtils增删改查的实现

package DButils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import pool.Customers;
import pool.DruidConnection;
import pool.JDBCUtils;

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

/**
 * @author 
 * @date 2021/4/8 15:24
 * 向里边传递的handle返回值就是query的返回值
 */
public class QueryRunnerTest {
    /**
     * @Author 小吕
     * @Description 没有返回结果的sql语句操作
     * @Date  2021/4/8 16:21
     **/
    @Test
    public void update() throws SQLException {
        QueryRunner runner = new QueryRunner();
        DruidConnection druidConnection = new DruidConnection();
        Connection connection = druidConnection.getConnection();
        String sql="UPDATE customers SET name =? WHERE id=?";
        int i = runner.update(connection, sql, "汪峰", 1);
        System.out.println(i);

    }
    /**
     * @Author 小吕
     * @Description 查询并返回单个结果
     * @Date  2021/4/8 16:20
     **/
    @Test
    public void select() throws SQLException {
        QueryRunner runner = new QueryRunner();
        DruidConnection druidConnection = new DruidConnection();
        Connection connection = druidConnection.getConnection();
        String sql="SELECT id,name,email,birth FROM customers where id=?";
        BeanHandler<Customers> handler=new BeanHandler<Customers>(Customers.class);
        Customers i = runner.query(connection,sql,handler,1);
        System.out.println(i);

    }
    /**
     * @Author 小吕
     * @Description 查询并返回列表
     * @Date  2021/4/8 16:20
     **/
    @Test
    public void select2() throws SQLException {
        QueryRunner runner = new QueryRunner();
        DruidConnection druidConnection = new DruidConnection();
        Connection connection = druidConnection.getConnection();
        String sql="SELECT id,name,email,birth FROM customers where id<?";
        BeanListHandler<Customers> handler= new BeanListHandler<>(Customers.class);
        List<Customers> i = runner.query(connection,sql,handler,19);
        i.forEach(System.out::println);
        JDBCUtils.closeResource(connection,null);
    }
    /**
     * @Author 小吕
     * @Description 查询特殊值操作
     * @Date  2021/4/8 16:20
     **/
    @Test
    public void select3() throws SQLException {
        QueryRunner runner = new QueryRunner();
        DruidConnection druidConnection = new DruidConnection();
        Connection connection = druidConnection.getConnection();
        String sql="SELECT max(birth)FROM customers";
        ScalarHandler scalarHandler=new ScalarHandler();
        Object date = runner.query(connection,sql,scalarHandler);
        System.out.println((Date)date);
        JDBCUtils.closeResource(connection,null);
    }
    /**
     * @Author 小吕
     * @Description 关于handle的理解
     * @Date  2021/4/8 16:29
     **/
    @Test
    public void select4() throws SQLException {
        QueryRunner runner = new QueryRunner();
        //获取druid链接
        DruidConnection druidConnection = new DruidConnection();
        Connection connection = druidConnection.getConnection();
        String sql="SELECT id,name,email,birth FROM customers where id=?";

        /**
         * 自定义handle类
         */
        ResultSetHandler<Customers> handler=new ResultSetHandler<Customers>() {
            @Override
            public Customers handle(ResultSet resultSet) throws SQLException {
                //方法体内写什么下边query就返回什么
                if (resultSet.next()){
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String email = resultSet.getString("email");
                    java.sql.Date birth = resultSet.getDate("birth");
                    return new Customers(id,name,email,birth);

                }
                return null;
            }
        };
        Customers customers = runner.query(connection,sql,handler,19);
        System.out.println(customers);
        DbUtils.close(connection);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值