在下面的代码片段中,我执行查询并将结果集传递给set
。然后set
被分配给set1
。之后有一个while循环直到set.next
返回false
。是否如果set.next
循环后返回false,set1
也将返回false?set1为空时为什么set1为空?
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 [email protected]@@");
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 [email protected]@@");
}
}
这while
结构永远不会奏效。这是原因吗?如果是这样,我该怎么办?
您已经遍历ResultSet。请理解,虽然有两个ResultSet *变量*,但它们都引用相同的ResultSet *对象*。所以如果你迭代了这个对象,并且在这个变量上调用'next()'返回false,那么无论你调用哪个变量,它都是false。 – 2013-02-08 18:19:56
@HovercraftFullOfEels但我在迭代之前将'set'分配给了'set1' – saplingPro 2013-02-08 18:22:23
没关系。再次set和set1指的是同一个对象。 – 2013-02-08 18:23:03