如何判断结果集是否为空?
再会!
我想知道如果我想确定我的搜索是否存在,如何在 if-else 语句
中获得所需的结果。我按照之前与此相关的问题中的建议进行了各种组合,但仍然无法得到我想要的。
代码 1:如果搜索为空,我无法到达 else 语句...
if (resultSet!= null) {
while (resultSet.next()) { //MULTIPLE VALUE SEARCH
Product product = new Product();
product.setProductId(resultSet.getInt("productId"));
product.setProductName(resultSet.getString("productName"));
productList.add(product);
}
request.setAttribute("productList", productList);
RequestDispatcher rd = request.getRequestDispatcher("adminViewSearch.jsp");
rd.forward(request, response);
} else {
request.setAttribute("message", "Search Failed"):
RequestDispatcher rd = request.getRequestDispatcher("adminViewSearch.jsp");
rd.forward(request, response);
}
代码 2:我也无法到达应显示搜索值的 else 语句... 代码
if (!resultSet.next()) {
.... Search Failed Message
} else {
while(result.next()){.....
}
3:结果与案例 1 相同:
if (resultSet.wasNull()) {
.... Search Failed Message
} else {
while(result.next()){.....
}
我尝试了其他组合,并且我仍然无法达到我想要的结果,如下所示: 当用户搜索一个值时,如果结果集为空,则会出现错误消息,否则如果搜索不为空,则会显示1个或多个搜索。
请给我一个更简单的替代方法,因为我似乎做错了。
先感谢您..
Good day!
I am wondering how can I get the desired result in my if-else statement
if i want to determine if my search is existing or not. I do various combinations as suggested in the previous questions related to this but still I cannot get what I want.
Code 1: I cannot reach the else statement if my search is empty...
if (resultSet!= null) {
while (resultSet.next()) { //MULTIPLE VALUE SEARCH
Product product = new Product();
product.setProductId(resultSet.getInt("productId"));
product.setProductName(resultSet.getString("productName"));
productList.add(product);
}
request.setAttribute("productList", productList);
RequestDispatcher rd = request.getRequestDispatcher("adminViewSearch.jsp");
rd.forward(request, response);
} else {
request.setAttribute("message", "Search Failed"):
RequestDispatcher rd = request.getRequestDispatcher("adminViewSearch.jsp");
rd.forward(request, response);
}
Code 2: I cannot also reach the else statement where values searched should be displayed...
if (!resultSet.next()) {
.... Search Failed Message
} else {
while(result.next()){.....
}
Code 3: Same result as Case 1:
if (resultSet.wasNull()) {
.... Search Failed Message
} else {
while(result.next()){.....
}
I tried other combinations and still I cannot achieve the result I desired which is as follows:
When the user search a value, if the resultset is null, an error message will appear, else if the search is not null, 1 or multiple searches will be displayed.
Kindly give me an alternative on how to this the easier way because it seems like I am doing it wrong.
Thank you in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在代码1中:
或者更合适的方式只需设置
并在
jsp
上in code 1:
Or more appropriate way would be just set
and on
jsp
一种可能的解决方案(尽管不是很优雅)是
但是,如果您无论如何都需要填充列表,也许 Jigar Joshi 的解决方案会更好。
One possible, though not very elegant, solution is
However, if you need to populate a list anyway, perhaps Jigar Joshi's solution would be better.
试试这个:
Try this :
您可以使用 ResultSet.getRow() 返回行数,如果返回零则表示没有产品。然后您就可以进行进一步的操作。
You can use ResultSet.getRow() which returns the number of rows and if it return zero than there are no products. And than you can carry out further operations.
使用可滚动的 ResultSet:
然后您可以调用
rs.beforeFirst( )
一直移回到结果集的顶部。Use a scrollable ResultSet:
You can then call
rs.beforeFirst()
to move all the way back to the top of the resultset.