Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate through the values of a row from a result set in java?

Tags:

java

sql

jsp

The result set I'm speaking of this: http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/ResultSet.html

What I would like to do is this...

for row in rows
    for col in row
        //col is the name of the column, row[col] is the value.

I'm more profecient in PHP, than JSP, fyi. This would be done in PHP like so:

foreach($rs as $row)
    foreach($row as $col => $val)
        //val is the cell value, and column is the column name

EDIT: I'm looking for a generic solution. notice how col is a variable, not a literal.

like image 508
Sophie McCarrell Avatar asked Jan 25 '12 20:01

Sophie McCarrell


People also ask

How do you iterate through ResultSet?

To iterate the ResultSet you use its next() method. The next() method returns true if the ResultSet has a next record, and moves the ResultSet to point to the next record. If there were no more records, next() returns false, and you can no longer.

How do I get only one row in a ResultSet?

You can use absolute to navigate to the first row: ResultSet rs = ...; rs. absolute(1); // Navigate to first row int id = rs.

Which command is used to check the next row in the ResultSet?

You can move the cursor of the ResultSet object to the next row from the current position, using the next() method of the ResultSet interface. This method returns a boolean value specifying whether the ResultSet object contains more rows.

Which of the following method in the ResultSet is used to retrieve data values?

The ResultSet interface declares getter methods (for example, getBoolean and getLong ) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column.


2 Answers

This is just a variation the a_horse_with_no_name answer. Here we use a List of List objects as suggested there.

final ResultSetMetaData meta = rs.getMetaData();
final int columnCount = meta.getColumnCount();
final List<List<String>> rowList = new LinkedList<List<String>>();
while (rs.next())
{
    final List<String> columnList = new LinkedList<String>();
    rowList.add(columnList);

    for (int column = 1; column <= columnCount; ++column) 
    {
        final Object value = rs.getObject(column);
        columnList.add(String.valueOf(value));
    }
}

// add the rowList to the request.

Edit Added final to all variables.

like image 101
DwB Avatar answered Oct 13 '22 17:10

DwB


ResultSetMetaData meta = rs.getMetaData();
int colCount = meta.getColumnCount();
while (rs.next())
{
    for (int col=1; col <= colCount; col++) 
    {
        Object value = rs.getObject(col);
        if (value != null) 
        {
            System.out.print(value.toString());
        }
    }
}

But I would not recommend to do something like this directly in the JSP page. Build up some kind of value holder (e.g. a List of Lists) in the backend and iterate over that.

like image 38
a_horse_with_no_name Avatar answered Oct 13 '22 19:10

a_horse_with_no_name