zoukankan      html  css  js  c++  java
  • java—DButils简化JBDC操作

    DButils实现增删改

    /**
     * 测试DButils的增删改
     */
    public class DBUtilsTest {
    
        /**
         * DButils 添加
         */
        @Test
        public void testadd(){
            try{
                QueryRunner qr = new QueryRunner();
                String sql = "insert into t1 value(?,?)";
                Object[] params = {9,"hhh"};
                Connection conn = C3P0utils.getConnection();
                int rows = qr.update(conn,sql,params);
                if (rows > 0) {
                    System.out.println("添加成功!");
                } else {
                    System.out.println("添加失败!");
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    
        /**
         * DButils 更改
         */
        @Test
        public void testupdate(){
            try{
                QueryRunner qr = new QueryRunner();
                String sql = "update t1 set name=? where id = ?";
                Object[] params = {"sxf",9};
                Connection conn = C3P0utils.getConnection();
                int rows = qr.update(conn,sql,params);
                if (rows > 0) {
                    System.out.println("修改成功!");
                } else {
                    System.out.println("修改失败!");
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    
        /**
         * DButils 删除
         */
        @Test
        public void testdel(){
            try{
                QueryRunner qr = new QueryRunner();
                String sql = "delete from t1 where id = ?";
                Object[] params = {7};
                Connection conn = C3P0utils.getConnection();
                int rows = qr.update(conn,sql,params);
                if (rows > 0) {
                    System.out.println("删除成功!");
                } else {
                    System.out.println("删除失败!");
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    }

    DButils 查询   

    BeanListHandler  查询所有装置list
    BeanHandler  指定第一天查询信息
    ScalarHandler  用于单数据操作
    public class DBUtilsTest2 {
        /**
         * 查询所有
         */
        @Test
        public void testQueryAll() {
            try{
                QueryRunner qr = new QueryRunner();
                String sql = "select * from t1";
                Connection conn = C3P0utils.getConnection();
                List<user> users = qr.query(conn,sql,new BeanListHandler<user>(user.class));
                for(user u:users){
                    System.out.println(u.getId() + " : " + u.getName());
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    
        /*
         * 根据id查询用户方法
         */
        @Test
        public void testQueryUserById() {
            try {
                // 1.获取核心类queryRunner
                QueryRunner qr = new QueryRunner(C3P0utils.getDataSource());
                // 2.编写sql语句
                String sql = "select * from t1 where id=?";
                //3.为占位符设置值
                Object[] params = {5};
                // 4.执行查询操作
                user user = qr.query(sql, new BeanHandler<user>(user.class), params);
                System.out.println(user.getId() + " : " + user.getName());
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
        /*
         * 根据所有用户的总个数
         */
        @Test
        public void testQueryCount() {
            try {
                // 1.获取核心类queryRunner
                QueryRunner qr = new QueryRunner(C3P0utils.getDataSource());
                // 2.编写sql语句
                String sql = "select count(*) from t1";
                // 4.执行查询操作
                Long count = (Long) qr.query(sql, new ScalarHandler());
                System.out.println(count);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    
    
    }
  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/zhuzhiwei-2019/p/11300618.html
Copyright © 2011-2022 走看看