欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JdbcBaseReview 系列之JdbcDBUtils(四)-----jbdc的jar包

程序员文章站 2022-06-02 17:03:47
...
public class Jdbc4DBUtils {

    //DbUtils是APache提供的一个开源的方便我们操作jdbc的jar包
    //QuerRunner类, 是该jar包的核心类 所有的操作数据库的方法都被封装在这个类中
    // 更新,查询都是使用QuerRunner类

    @Test
    public void t1() throws SQLException {
        //获得连接对象
        Connection conn = JdbcUtil.getConnection();
        //建立一个QueryRunner对象,用来执行sql 语句
        QueryRunner qr = new QueryRunner();

        String sql = "insert into stu values(null,'妲己',12)";

        //调用update方法,可以执行 增 删 改 等sql语句
        qr.update(conn, sql);

        conn.close();

    }

    @Test
    public void t2() throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        QueryRunner qr = new QueryRunner();
        String queryOne = "select * from stu where id=8 ";
        Stu query = qr.query(conn, queryOne, new BeanHandler<Stu>(Stu.class));

        System.out.println(query);

    }

    //演示BeanListHandler的使用
    //使用BeanListHandler可以得到一个装载指定类型对象的集合数组
    //@throws SQLException
    @Test
    public void t3() throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        QueryRunner qr = new QueryRunner();
        String queryAll = "select name, age from stu";

        List<Stu> stus = qr.query(conn, queryAll, new BeanListHandler<Stu>(Stu.class));
        //手敲四个字母iter + tab键
        for (Stu stu : stus) {
            System.out.println(stu.toString());
        }

    }

    @Test
    public void t4() throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        QueryRunner qr = new QueryRunner();
        String queryAll = "select name, age from stu";
        List<Map<String, Object>> query = qr.query(conn, queryAll, new MapListHandler());
        System.out.println(query);

        //手敲四个字母iter + tab键
        for (Map<String, Object> stringObjectMap : query) {
            for (String s : stringObjectMap.keySet()) {
                System.out.println(s);
            }
        }

    }

    @Test
    public void t5() throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        QueryRunner qr = new QueryRunner();
        String queryAll = "select *from stu";
        List<Object[]> query = qr.query(conn, queryAll, new ArrayListHandler());

//        System.out.println(query);
        for (Object[] objects : query) {
            for (Object object : objects) {
                System.out.printf(object + "-");
            }
            System.out.println();
        }
    }

    /*
     ResultSetHandler是接口
     BeanListHandler, BeanHandler等类都实现了ResultSetHandler接口
     @throws SQLException
     */
    @Test
    public void t6() throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        QueryRunner qr = new QueryRunner();
        String queryAll = "select *from stu where id=8";
        qr.query(conn, queryAll, new ResultSetHandler<Stu>() {
            @Override
            public Stu handle(ResultSet resultSet) throws SQLException {
                while (resultSet.next()) {
                    Stu stu = new Stu();
                    stu.setName(resultSet.getString(2));
                    stu.setAge(resultSet.getInt(3));
                    stu.setId(resultSet.getInt(1));
                    System.out.println(stu);
                    return stu;
                }
                return null;
            }
        });
    }
}