将 ResultSet 中的特定记录分配给变量

发布于 2024-07-11 12:04:28 字数 192 浏览 5 评论 0原文

我想从数据库中检索一组记录,执行 rs.next() ,然后将其结果分配给一个变量以传递给将使用该记录的方法,就像我不分配它一样到一个变量并将其传递给一个方法 有什么办法可以做到这一点吗? 我正在使用 JAVA (1.5)


感谢您提供的所有答案

我不想将整个结果集传递给方法,仅传递当前行,但据我了解,不可能这样做

I want to retrieve a set of records from a database, do a rs.next() and then assign the result of this to a variable to pass to a method that will use this record, in the same way that I would without assigning it to a variable and passing it to a method
is there any way to do this?
I'm using JAVA (1.5)


Thank you for all the answers

I do not want to pass the whole resultSet to a method, only the current row, but as I understand, it is not possible to do this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除→记忆 2024-07-18 12:04:28

不,不支持开箱即用,但也许以下想法可能对您有所帮助:

ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
  Map<String,Object> row = new HashMap<String,Object>();
  for (int i = 1; i <= meta.getColumnCount(); ++i) {
    row.put(meta.getColumnName(i), rs.getObject(i));
  }
  processRow(row);
}

问题是,您需要从 processRow()< 中的 row-map 转换值/code> 并且它不适用于所有类型/驱动程序组合(BLOB,...)。

No, that is not supported out of the box, but maybe the following idea may help you:

ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
  Map<String,Object> row = new HashMap<String,Object>();
  for (int i = 1; i <= meta.getColumnCount(); ++i) {
    row.put(meta.getColumnName(i), rs.getObject(i));
  }
  processRow(row);
}

The problem is, you need to cast the values from the row-map in processRow() and it will not work for all type/driver combinations (BLOBs, ...).

九局 2024-07-18 12:04:28

您想将rs.next()的结果分配给一个变量吗?

恐怕你做不到。 您可以从该 rs 获得引用并在其他方法中处理它(与不将其分配给 var 的方式相同)

当您将 rs 作为参数传递给方法时,这正是您已经在做的事情。

while( rs.next() ) { 
    list.add( createFrom( rs ) );
}

...

public Object createFrom( ResultSet rs ) {  // Here rs is a copy of the reference in the while above. 
}

我答对了你的问题吗?

You want to assign the result of rs.next() to a variable?

I'm afraid you cannot do that. You can have a reference from that rs and process it in other method ( the same way you would without assign it to a var )

When you pass the rs as an argument to a method that is exactly what you're doing already.

while( rs.next() ) { 
    list.add( createFrom( rs ) );
}

...

public Object createFrom( ResultSet rs ) {  // Here rs is a copy of the reference in the while above. 
}

Did I get right your question?

℉絮湮 2024-07-18 12:04:28

我想我现在理解你了。 :)

不,不可能那样做。 ResultSet 在每次调用 ResultSet.next() 时都会更改其内部状态,因此存储对它的引用不会给您带来任何帮助。 您必须将所需的数据提取到某个自定义对象中并将其存储。

CustomData cd = new CustomData();
cd.setString1(rs.getString(1));
cd.setInt1(rs.getInt(2));
...

然后,您可以存储 cd 变量并继续处理 ResultSet

I think I understood you now. :)

No, it’s not possible to do it that way. The ResultSet changes its internal state on each call to ResultSet.next() so storing the reference to it won’t get you anywhere. You have to extract the data you want into some custom object and store that.

CustomData cd = new CustomData();
cd.setString1(rs.getString(1));
cd.setInt1(rs.getInt(2));
...

You can then store the cd variable and get on with processing your ResultSet.

柒七 2024-07-18 12:04:28

我经常发现List> 数据结构在处理结果集时非常有用。

I often find List> data structure very useful while working with result sets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文