Java JDBC指向前一个结果集的游标

时间:2018-05-20 12:48:22

标签: java jdbc

当我打破while循环时,我必须将光标位置指回到前一个位置。

下面是我的while循环代码:

while (rs.next()) {
    docId = rs.getString("DOCID");
    System.out.println("In : extractData(1st):docId"+docId);
    // if previous id is not match with the current id then exitfrom while loop.
    if(previousDocId != null && !previousDocId.equals(docId)){
        //move cruser back.
        break;
    }
    thisIsAStringArray[0] = firstDocId;
    String name1 = rs.getString("field_name");
    System.out.println("field Name:"+name1);
    if("address".equals(name1) && 1 == rs.getInt("field_type")){
        thisIsAStringArray[1] = rs.getString("field_string");
    }
    // 
    previousDocId = docId;
}

请指教。

1 个答案:

答案 0 :(得分:0)

获得ResultSet后,您必须为其设置属性ResultSet.TYPE_SCROLL_INSENSITIVEResultSet.TYPE_SCROLL_SENSITIVE才能使其可滚动

假设您从ResultSet获得了Statement

Statement statement = conn.createStatement(
   ResultSet.TYPE_SCROLL_INSENSITIVE // scrollable    
);
ResultSet rs = statement.executeQuery(your query);

并使用rs.previous()向后移动。