我正在执行语句查询并将数据存储在 ResultSet rs 中。
// if no record is returned by the query
if (!rs.next() ) {
Step1
}
// if atleast one record is returned by the query
if(rs.next()){
do {
if (rs.getString(1).equalsIgnoreCase("0")){
Step2
}
if (rs.getString(1).equalsIgnoreCase("1")){
Step3
}
} while (rs.next());
}
但是,如果我只从查询中获取一条记录,则不会执行任何步骤。如果有人能指出错误,将会有很大帮助。
最佳答案
您必须了解 next()
方法到底做什么。 next() 将光标移动到下一个元素。当您在 if 中编写 next()
时,条件已通过,并且在 while
中没有更多元素。
为什么不通过这样做来简化代码
while ( rs.next() ) {
if (rs.getString(1).equalsIgnoreCase("0")){
Step2
}
if (rs.getString(1).equalsIgnoreCase("1")){
Step3
}
}
如果进入while循环,则有项目,否则没有。
关于java - 关于 ResultSet 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221878/