rset.next() (1) Retreives the next tuple in result set object rset (2) Method returns null if no more tuples in rset |
while ( rset.next() != null ) { // Use rset object to access the attribute values // in the current tuple in the result set rset.getString(1); rset.getInt(2); etc, etc } |
|
ResultSet rset; // Result set (cursor) rset = SQLstatement.executeQuery("some SQL-query"); while ( rset.next () ) { String stringVar = rset.getString(index); int intVar = rset.getInt(index); long longVar = rset.getLong(index); float floatVar = rset.getFloat(index); double doubleVar = rset.getDouble(index); (there are many more other flavor of get() that I omitted....) } |
Example: print the first 8 attributes in every tuple
while ( rset.next () ) // rset will loop through every tuple in result { System.out.println ( rset.getString(1) + " " // Get 1st attribute value in tuple + rset.getString(2) + " " // Get 2nd attribute value in tuple + rset.getString(3) + " " // Get 3rd attribute value in tuple + rset.getString(4) + " " // And so on + rset.getString(5) + " " + rset.getString(6) + " " + rset.getString(7) + " " + rset.getString(8) ); } |
|
// Assume the we have executed a query and // rset contains the result set /* ================================================= Retrive all tuples in the result set ================================================= */ while ( rset.next () ) // Get next tuple in result set rset { /* ====================================================== Print the retrieved attributes from the current tuple ====================================================== */ System.out.println ( rset.getString(1) + " " // Get 1st attribute value in tuple + rset.getString(2) + " " // Get 2nd attribute value in tuple + rset.getString(3) + " " // Get 3rd attribute value in tuple + rset.getString(4) + " " // And so on + rset.getString(5) + " " + rset.getString(6) + " " + rset.getString(7) + " " + rset.getString(8) // I "blindly" retrieve 8 attribute values... ); } /* ============================================================= After you finish using rset, you should "destroy" the object ============================================================= */ rset.close(); // Destroy the result set object // It's "OK" if you don't do this // Then Java will then clean it up later // through its garbage collection mechanism... |
Note:
|