我想通过一个开关控制遍历结果集的方向
if (ok){
    ResultSet rs = setFetchDirection(ResultSet.FETCH_REVERSE);
}
下面怎么遍历呢 
while (rs.next())??
while (rs.previous())???

解决方案 »

  1.   

    create table test
    (id ,number(10));
    insert into test
    select rownum from dual connect by rownum<10;
    select * from test order by id asc;
    然后自己试下不就知道了
      

  2.   

    if (ok){
      ResultSet rs = setFetchDirection(ResultSet.FETCH_REVERSE);
      while (rs.next()){
       //从结果集中取结果就行了。
       //rs.getString("xxxx"); or rs.getInt(数字); 
      }
    }
      

  3.   

    FETCH_REVERSE
    static final int FETCH_REVERSE该常量指示将按逆向(即从最后一个到第一个)处理结果集中的行处理。setFetchDirection 方法将此常量用作驱动程序的提示,驱动程序可能忽略它。所以应该是while (rs.previous())
      

  4.   

    while (rs.next())
    while (rs.previous())我都试过了  
    都不好使  你信不信  不信你也试试while (rs.next())  是正向的遍历while (rs.previous())  结果都有错
      

  5.   

    有数据 通过
    rs.afterLast();
    while (rs.previous()) {可以解决 逆向的  但是 
     里边遍历的内容 得重复写  两遍 (再写一遍正常顺序的)
    里面的代码有很多
    看到设置ResultSet rs = setFetchDirection(ResultSet.FETCH_REVERSE);
    以为  可以通过
    if (ok){
      ResultSet rs = setFetchDirection(ResultSet.FETCH_REVERSE);
    }
    再加上其它设置(未知)
    其它代码不变 就解决问题呢
    搜了很多 都和API 介绍的一样 
    没有一个实际的例子
      

  6.   

    可以这样
    rs.afterLast();
    while(rs.previous()) {
    System.out.println(rs.getInt(1));
    }
      

  7.   

    取到 数据    
    保存为  什么形式呢  map ? set?
    因为每条记录 都要取好几个值
    感觉怎么遍历都不方便
      

  8.   


    保存成 List<list<?>> 这样的类型就行了。
    要不按照 List<Object[]>这样也行hibernate就是这样封装的。
      

  9.   

    try {
        // Create a scrollable result set
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");    // Move cursor forward
        while (resultSet.next()) {
            // Get data at cursor
            String s = resultSet.getString(1);
        }    // Move cursor backward
        while (resultSet.previous()) {
            // Get data at cursor
            String s = resultSet.getString(1);
        }    // Move cursor to the first row
        resultSet.first();    // Move cursor to the last row
        resultSet.last();    // Move cursor to the end, after the last row
        resultSet.afterLast();    // Move cursor to the beginning, before the first row.
        // cursor position is 0.
        resultSet.beforeFirst();    // Move cursor to the second row
        resultSet.absolute(2);    // Move cursor to the last row
        resultSet.absolute(-1);    // Move cursor to the second last row
        resultSet.absolute(-2);    // Move cursor down 5 rows from the current row.  If this moves
        // cursor beyond the last row, cursor is put after the last row
        resultSet.relative(5);    // Move cursor up 3 rows from the current row.  If this moves
        // cursor beyond the first row, cursor is put before the first row
        resultSet.relative(-3);
    } catch (SQLException e) {
    }
      

  10.   

    那还是 和 Hibernate学学吧  
    谢谢了试试先
      

  11.   

    while(rs.next())
    集合.add(obj)
    最后吧集合排序
    最后for输出集合内容
      

  12.   

    ChDw说的应该可行:
    可以这样
    rslast
    while(rs.previous()) {
    System.out.println(rs.getInt(1));
    }
      

  13.   


    可以是可以 
    不过 我输入的参数 如果要求正常顺序遍历呢 ?
    再写一个?其实 最好是 
    if (ok){
      rs.setFetchDirection(ResultSet.FETCH_REVERSE);
    }
    while (rs.next()) {}
    如果OK = TRUE 
    rs.next() 就是逆向的遍历  那该多好  呵呵呵 
     
    谁能说说 rs.setFetchDirection(ResultSet.FETCH_REVERSE); 到底有什么用?????
      

  14.   

    最好的:先存入list,然后用java的list倒序即可。在数据库中倒序,速度太慢。没有上述在内存中倒序快