如何确定结果集是否为空? [英] How to determine if resultset is empty?

查看:83
本文介绍了如何确定结果集是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我想知道如果我想确定搜索是否存在,如何在if-else statement中获得所需的结果.我按照前面有关此问题的建议进行了各种组合,但仍然无法获得想要的东西.

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.

代码1:如果我的搜索为空,则无法到达else语句...

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);
        }

代码2:我也无法到达else语句,其中应显示搜索到的值...

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()){.....
        }

代码3:与案例1相同的结果

Code 3: Same result as Case 1:

        if (resultSet.wasNull()) {
             .... Search Failed Message    
        } else { 
             while(result.next()){.....
        }

我尝试了其他组合,但仍然无法达到我想要的结果,如下所示: 当用户搜索一个值时,如果结果集为null,则将显示一条错误消息,否则,如果搜索不为null,则将显示1个或多个搜索.

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.

先谢谢您.

推荐答案

在代码1中:

    if(productList.size() == 0 ){
         request.setAttribute("message", "Search Failed"):        
    }else{
          request.setAttribute("productList", productList);             
    }

    RequestDispatcher rd = request.getRequestDispatcher("adminViewSearch.jsp");
    rd.forward(request, response);  


或者更合适的方式将被设置


Or more appropriate way would be just set

    if (resultSet!= null) {
        while (resultSet.next()) { 
            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);
    }

jsp

<c:if test="${fn:length(companies) gt 0}">
   <!--display collection using c:foreach -->
</c:if>

<c:if test="${fn:length(companies) eq 0}">
   Search failed
</c:if>

这篇关于如何确定结果集是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆