DBUtils之查询(二)

  • 使用ColumnListHandler
public static void columnListHandler() throws SQLException {
        Connection con = MyJDBCUtiles.getConnection();
        QueryRunner qr = new QueryRunner();
        String sql = "select username,password from userinfo where age = ?";
        //注意sql语句中的筛选项(username,password)一定要包括下列list中的参数项(username)
        Object [] param = {29};
        List<Object> list = qr.query(con,sql,new ColumnListHandler<Object>("username"),param);
        //若没有查询到则list.size()=0
        for (Object o : list){
            System.out.println(o + "\t");
        }
    }
  • 结果

  • 使用ScalarHandler
public static void scalarHandler() throws SQLException {
        Connection con = MyJDBCUtiles.getConnection();
        String sql = "select count(*) from userinfo where age = ?";
        QueryRunner qr = new QueryRunner();
        Object [] param = {21};
        long o = qr.query(con,sql,new ScalarHandler<Long>(),param);
        //若未查询到,则o的值为0
        System.out.println(o);
    }
  • 结果

猜你喜欢

转载自www.cnblogs.com/kongieg/p/10066451.html