利用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);
        }
    }
}

你可能感兴趣的:(JDBC学习(MySQL),数据库,mysql,java,jdbc,dbutils)