QueryRunner实现查询操作

public static  void insert() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="insert into sort values(?,?,?)";        
        Object[] obj ={9,"橡皮擦","好吃又大补"};
        Connection conn =JDBCUtils.getConnection();
        int row= qr.update(conn,sql,obj);
        conn.close();
        System.out.println(row);
    }
    public static void update() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="update sort set sname=? , sdesc =? where sid =?";
        Object[] obj ={"排骨","好吃",9,};
        Connection conn =JDBCUtils.getConnection();
        int row= qr.update(conn,sql,obj);
        conn.close();
        System.out.println(row);
    }
    public static void delect() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="delete from sort  where sid =?";        
        Connection conn =JDBCUtils.getConnection();
        int row= qr.update(conn,sql,9);
        conn.close();
        System.out.println(row);
    }
    public static void find1() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //获取第一条记录
        Object[] obj =qr.query(conn, sql,new ArrayHandler());
        for(Object o:obj){
            System.out.print(o+"...");
        }
    }
    public static void find2() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //获取全部记录
        List<Object[]> list =qr.query(conn, sql,new ArrayListHandler());
        for(Object[] obj:list){
            for(Object o1:obj){
                System.out.print(o1+"...");
            }
            System.out.println();
        }
    }
    public static void find3() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //获取第一条记录(封装成对象)
        sort s =qr.query(conn,sql,new BeanHandler<sort>(sort.class));
        System.out.println(s);
    }
    public static void find4() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //获取所有记录(封装成对象)
        List<sort> list =qr.query(conn,sql,new BeanListHandler<sort>(sort.class));
        for(sort s:list){
            System.out.println(s);
        }
    }
    public static void find5() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //获取sname列所有数据
        List<String> list =qr.query(conn,sql,new ColumnListHandler<String>("sname"));
        for(String s:list){
            System.out.println(s);
        }
        conn.close();
    }
    public static void find6() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select count(*) from sort";        
        Connection conn =JDBCUtils.getConnection();
        //用于单数据。例如select count(*) from 表操作。
        Long count =qr.query(conn,sql,new ScalarHandler<Long>());                    
        System.out.println(count);
        conn.close();
    }
    public static void find7() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //将结果集第一行封装到Map集合中,Key 列名, Value 该列数据
        Map<String ,Object> map =qr.query(conn,sql,new MapHandler());                    
        for(String key:map.keySet()){
            System.out.print(key+"..."+map.get(key));
        }
        conn.close();
    }
    public static void find8() throws SQLException{
        QueryRunner qr =new QueryRunner();
        String sql ="select * from sort";        
        Connection conn =JDBCUtils.getConnection();
        //将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合
        List<Map<String ,Object>> list =qr.query(conn,sql,new MapListHandler());                    
        for(Map<String ,Object> m:list){
            for(String s:m.keySet()){
                System.out.print(s+"..."+m.get(s));
            }
            System.out.println();
        }
        conn.close();
    }

 

posted @ 2018-07-12 16:14  淅沥沥丶下雨  阅读(4518)  评论(0编辑  收藏  举报