[英]Why does set1 get null when set goes null?
In the following snippet I execute a query and pass the result set to set
. 在以下代码段中,我执行一个查询并将结果集传递给
set
。 Then the set
is assigned to set1
. 然后将
set
分配给set1
。 After that there is a while loop which goes on until set.next
returns false
. 之后,将进行while循环,直到
set.next
返回false
为止。 Is it that if set.next
returns false after the loop, set1
will also return 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
}
}
I asked this because: 我之所以这样问是因为:
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 !@@@");
}
}
This while
construct never works. 这
while
构造永远行不通。 Is that the reason ? 那是原因吗? If that is, what do I do ?
如果是的话,我该怎么办?
You have two major bugs: 您有两个主要错误:
set
to set1
doesn't make a copy - it's the same set set
分配给set1
不会复制-它是相同的set String.contains()
is case sensitive (your code doesn't match your comments) String.contains()
区分大小写 (您的代码与您的注释不匹配) The fix is: 解决方法是:
`toLowerCase()
with contains()
to achieve a "case insensitive contains" test `toLowerCase()
与contains()
可实现“不区分大小写的包含”测试 Try this code: 试试这个代码:
ResultSet set = statement.executeQuery();
while(set.next()) {
if(set.getString(1).toLowerCase()
.contains(keyword.toLowerCase)) {
list.put(set.getString(2), set.getString(1));
}
}
Also, don't call a map "list" - call it "map", otherwise it's just going to confuse readers of your code. 另外,请勿将地图称为“列表”,而将其称为“地图”,否则只会使您的代码读者感到困惑。
您可以尝试这样做。
ResultSet set1 = statement.executeQuery();
set1
is a reference to the same object that set
points to. set1
是对set
指向的同一对象的引用。 If you want to iterate over the ResultSet
again, you need to move the iterator cursor to the beginning again using something like: 如果要再次遍历
ResultSet
,则需要使用以下类似的方法将迭代器光标再次移至开头:
set.beforeFirst();
In Java, assigning a variable to an object doesn't make a copy of the object, it only references the object in memory. 在Java中,为对象分配变量不会复制该对象,它只会引用内存中的对象。 You would have to explicitly make a copy of the object if you wanted
set
and set1
to work independently. 如果要让
set
和set1
独立工作,则必须显式地创建对象的副本。
In your particular use case, I don't think that's what you want, it seems better to just move the iteration cursor. 在您的特定用例中,我认为这不是您想要的,只是移动迭代光标似乎更好。
Is it that if
set.next
returns false after the loop,set1
will also return false ?如果
set.next
在循环后返回false,则set1
也将返回false吗?
YES . 是的 。 Because
set1
is referring the same object of Resultset
which set
is referring. 因为
set1
引用了set
所引用的Resultset
的相同对象。 So whatever behavior is exhibited by the ResultSet
object referenced by set
will also be reflected by set1
.In your first while loop the ResultSet
object referenced by set
is exhausted completely after iterating to all records (using set.next()) . 因此,
set1
也会反映set
引用的ResultSet
对象表现出的任何行为。在您的第一个while循环中, set
引用的ResultSet
对象在迭代所有记录之后(使用set.next())会完全耗尽。 Thereafter no matter which variable (set or set1) tries to read more both will get .next()
as false
. 此后,无论哪个变量(set或set1)尝试读取更多内容,两者都将
.next()
为false
。
If in Your first while loop construct I check .next()
using set
variable and get the result using set1
variable it still will work. 如果在您的第一个while循环构造中,我使用
set
变量检查.next()
并使用set1
变量获取结果,它仍然可以工作。 Try this code instead of your first construct and you will see that output is same as what you have written in your first while construct: 尝试使用以下代码而不是第一个构造,您将看到输出与您在第一个while构造中编写的输出相同:
ResultSet set = statement.executeQuery();
ResultSet set1 = set;
while(set.next()) {
if(set1.getString(1).equalsIgnoreCase(keyword)) { // If the search matches the exact keyword
list.put(set1.getString(2), set1.getString(1));
// Where key is the name of the node and value is, name of the file
}
}
If you still want set1
to return results you should reconstruct a new ResultSet
object using : 如果仍然希望
set1
返回结果,则应使用以下命令重建一个新的ResultSet
对象:
set1 = stat.executeQuery()
where, stat
is the Statement/PreparedStatement
whatever you are using in your code. 其中,
stat
是您在代码中使用的Statement/PreparedStatement
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.