如何在java中的结果集中迭代行的值? [英] How do I iterate through the values of a row from a result set in java?

查看:114
本文介绍了如何在java中的结果集中迭代行的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果集我说的是:
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.

我在PHP中比JSP,fyi更有资格。这将在PHP中完成,如下所示:

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

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

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

推荐答案

这只是一个变体 a_horse_with_no_name回答
这里我们使用 列出 列出对象。

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 (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。

Edit Added final to all variables.

这篇关于如何在java中的结果集中迭代行的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆