简体   繁体   中英

Select records from ResultSet java

I have a long query in Java which is put in a result set and executed by the executeQuery(string) method. Now I need to traverse this result set, query each of the records in the result set (method get_linked_OrgTickets contains this query) and output only those rows which return something in Vector linkedToTicket from the ResultSet. Can I sort of traverse this ResultSet and copy the said rows into another result set or maybe remove the rows not to be outputted from the ResultSet? Is there another way to solve this?

if (linked == J.LINKED_TO_ORG){
    while (rs.next()){
           Vector linkedToTicket = Ticket.get_linked_OrgTickets(con, rs.getInt(Ticket.FLD_ID)); 

           if (linkedToTicket != null)
           {                                
          // if there are linked tickets in the result set, output them only

        }
    }
}

* EDIT: * These subqueries can't be integrated with the main query. The only way to do that is to execute the main query once without this and the second time with it included which would be too inefficient. ResultSet class does not seem to contain methods for deleting a row without affecting the database or for building a ResultSet 'manually'.

Please help!

Maybe you could manipulate the cursor in the resulset and pickup the right data. Something like this

Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, 
            ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery(
            " SELECT col1 FROM tablename");
rs.absolute(5); // moves the cursor to the fifth row of rs
String field = rs.getString("MY_FIELD");

however, be careful not to call updaterow because you will modify the underlying data row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM