How to put a result set in an array to use multiple times

I have a resultSet that I am retrieving from my database:

      String selStmt = "SELECT code_id, internal_code, representation, pos, decode(substr(internal_code, 5, 1), 'Q', 2, 1) as sorter, "
                 + "       to_char(term_start, 'MM-DD-YYYY') as sDate "
                 + "  FROM table1, terms WHERE code_id = 'SEARCH.TERMS' AND internal_code = terms.terms_id (+) ORDER BY 5, 4 ";

  stmt = conn.prepareStatement(selStmt);
  result = stmt.executeQuery();

Now I want to make these results into an array, I know that I can use the while loop to loop and get the results, but I can use them only once, I need to continue to use them throughout the rest of the page. I realized that this can be done using an array, but if there is an easier way, let me know, also if you need more information, please let me know.

+3
source share
7 answers

There is no DataSetJava equivalent (.NET) that I know of. Therefore, consider the following:

ArrayList<Integer> data = new ArrayList<Integer>();

ResultSet rs = ....;

// For each record in the result...    
while (rs.next()) {
  // Want more values? Create a custom Type representing the Row!
  // The following is just an example taking the first column (as an int).
  // This would be done manually for each column... and possibly loadeded
  // in a custom object which is then added to the list... blah blah.
  // (Alternatively each row could be represented as Object[] or List<Object>
  //  at the expense of losing static typing.)
  int id = rs.getInt(0);
  data.add(id);
}

// Then later, if you *really* want an array...
// (Java is such a backwards language and lacks a trivial way
//  to go to int[] from Integer[] but I digress...)
Integer[] array = data.toArray(new Integer[0]);

Happy coding

+1

, ArrayList. ( ).

+2

2D-. , , ResultSet.

, ResultSet, , .

+1

- ResultSet () , . , , .

, (-) "" , ResultSet next() ( ArrayList ResultSet ). , , , . ResultSet JDBC, . ResultSet , , . "" Java ( rs.getXXX()), JDBC api. , (, iBatis), , , - .

, - - , ():

   public ResultSet getUsers() { 
      conn = openConnection();
      stmt = conn.prepareStatement(...);
      result = stmt.executeQuery();
      stmt.close();
      conn.close();
      return result;
   }

. , , db ( !).

- , RowSet. , .

+1

:

Vector rows = new Vector();
Vector nrow;
int cnt = 0;

while(result.next())
{
    nrow=new Vector();
    cnt+=1;
    nrow.addElement(String.valueOf(cnt));
    for(int i=1;i<=3;i++) //replace 3 with the length of the columns
    {
        nrow.addElement(result.getObject(i));
    }
    rows.addElement(nrow);
}

. , , .

+1

, , . while, while :)

0

Another option is to use HashMap to store your lines if you need to constantly refer to them throughout the page. For the index, use the primary key from db.

This way, you can quickly refer to specific lines as needed. If you just need to loop the result set every time the arrarialist, as has been said, should work very well here.

0
source

All Articles