将 ResultSet 中的特定记录分配给变量
我想从数据库中检索一组记录,执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,不支持开箱即用,但也许以下想法可能对您有所帮助:
问题是,您需要从
processRow()< 中的
row
-map 转换值/code> 并且它不适用于所有类型/驱动程序组合(BLOB,...)。No, that is not supported out of the box, but maybe the following idea may help you:
The problem is, you need to cast the values from the
row
-map inprocessRow()
and it will not work for all type/driver combinations (BLOBs, ...).您想将rs.next()的结果分配给一个变量吗?
恐怕你做不到。 您可以从该 rs 获得引用并在其他方法中处理它(与不将其分配给 var 的方式相同)
当您将 rs 作为参数传递给方法时,这正是您已经在做的事情。
我答对了你的问题吗?
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.
Did I get right your question?
我想我现在理解你了。 :)
不,不可能那样做。
ResultSet
在每次调用ResultSet.next()
时都会更改其内部状态,因此存储对它的引用不会给您带来任何帮助。 您必须将所需的数据提取到某个自定义对象中并将其存储。然后,您可以存储
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 toResultSet.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.You can then store the
cd
variable and get on with processing yourResultSet
.我经常发现List> 数据结构在处理结果集时非常有用。
I often find List> data structure very useful while working with result sets.