【发布时间】:2013-02-08 18:17:52
【问题描述】:
在下面的 sn-p 中,我执行一个查询并将结果集传递给set。然后将set 分配给set1。之后有一个while循环,直到set.next返回false。是不是set.next循环后返回false,set1也会返回false?
ResultSet set = statement.executeQuery();
ResultSet set1 = set;
while (set.next()) {
if (set.getString(1).equalsIgnoreCase(keyword)) {
// If the search matches the exact keyword
list.put(set.getString(2), set.getString(1));
// Where key is the name of the node and value is, name of the file
}
}
我问这个是因为:
while (set1.next()) {
System.out.println("@@@Inside the while statement@@@");
if (set1.getString(1).contains(keyword)
&& set1.getString(1).compareToIgnoreCase(keyword) !=0) {
// If the search contains the keyword but does not exactly match
list.put(set.getString(2), set.getString(1));
// Where key is the name of the node and value is, name of the file
System.out.println("@@@Inside the if statement of second while loop !@@@");
}
}
这个while 构造永远不会起作用。是这个原因吗?如果是这样,我该怎么办?
【问题讨论】:
-
您已经遍历了 ResultSet。请理解,虽然您有两个 ResultSet 变量,但它们都指向同一个 ResultSet object。因此,如果您已遍历对象并在一个变量上调用
next()返回 false,那么无论您在哪个变量上调用该方法,它都会为 false。 -
@HovercraftFullOfEels 但我在迭代之前将
set分配给set1 -
没关系。同样 set 和 set1 指的是同一个对象。
-
@HovercraftFullOfEels 那么有没有办法复制对象以保持他们的灵魂不同?