在下面的代碼片段中,我執行查詢並將結果集傳遞給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