小编典典

如何在Java中从结果集中迭代行的值?

jsp

我所说的结果集:http
:
//docs.oracle.com/javase/1.4.2/docs/api/java/sql/ResultSet.html

我想做的是…

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

我比PHP更熟练于PHP,而不是JSP。可以这样在PHP中完成:

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

编辑:我正在寻找一个通用的解决方案。注意col是变量,而不是文字。


阅读 323

收藏
2020-06-08

共1个答案

小编典典

这只是a_horse_with_no_name答案的一种变体。在这里,我们使用此处建议ListList对象。

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 (final int column = 1; column <= columnCount; ++column) 
    {
        final Object value = rs.getObject(column);
        columnList.add(String.valueOf(value));
    }
}

// add the rowList to the request.

编辑将 final添加到所有变量。

2020-06-08