各位兄弟姐妹,这个程序再oracle、mysql上执行没任何问题
其中:$where为条件,currentPage为当前页码,totalRecords为记录总数,numPerPage为页记录数
在oracle、mysql上可以返回正确的记录总数,但在sqlserver上返回记录总数为-3,是何原因啊?
应该如何去改正呢?谢谢各位了!
public static List query(String $where, Integer currentPage,
                           AIInteger totalRecords, int numPerPage) {
    List templist = new ArrayList();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = MyDBHelper.getConnection();
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);
      rs = stmt.executeQuery(sql);
      rs.last();                                                      //1>处
      totalRecords.setValue(rs.getRow());                             //2>处
      System.out.println("current page:= " + currentPage.toString() +
                         "; total:= " +
                         totalRecords + "; numPerPage:= " + numPerPage);
      if (currentPage.intValue() > 1)
        rs.absolute( (currentPage.intValue() - 1) * numPerPage);
      else
        rs.beforeFirst();
      rs.setFetchSize(numPerPage);      while (rs.next()) {
        、
        、
        、
        tempList.add(temp);
        numPerPage--;
      }
      rs.close();
      stmt.close();
    }
    catch (SQLException ex) {
      while (ex != null) {
        System.out.println("Message :" + ex.getMessage());      
      }
    }
    catch (java.lang.Exception ex) {
      ex.printStackTrace();
    }
    //释放一个连接到连接池
    finally {
      try {
        conn.close();
      }
      catch (SQLException sqle) {
      }
    }
    return tempList;
  }

解决方案 »

  1.   

    sqlserver uses top clause!
    mysql uses limit clause!
    oracle uses row_num functionusing cursor is unefficient
      

  2.   

    有可能是rs.last();的问题
    你试试next循环计数
      

  3.   

    csdn有多个关于rs.first(), rs.last()问题的
    它有时总不能得到期望的结果(与数据库有关,与数据库返回类型表关,如表或光标)
    这个好像都没有一个合理的解释或解决方案
    要解决你的问题,可用
      select count(*) recc form 表 where 条件
    来先预查记录数
      

  4.   

    获得resultset之后
    int counter = 0;
    while(resultset.next())
     counter++;来获得总记录数,或者也可以参考楼上的方法
    last、first等方法经常会有问题