How to determine if resultset is empty?

23,105

Solution 1

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

and on 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>

Solution 2

One possible, though not very elegant, solution is

if (!resultSet.next()) {
     .... Search Failed Message
} else {
    do {
        ...
    } while (resultSet.next());
}

However, if you need to populate a list anyway, perhaps Jigar Joshi's solution would be better.

Solution 3

Try this :

boolean searchEmpty = true;

while (resultSet.next()) {
  //create objects
  searchEmpty = false;
}

if (searchEmpty) {
 //set error message.
}

Solution 4

  if(rs !=null)
   {
    if(rs.isBeforeFirst() && rs.isAfetrLast())
     {
     out.println("row is empty");
      }
     else
    {
     while(rs.next())
     {

   //execute your code 
   }
 }
 }
else{ out.println("row is null");}

Solution 5

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.

Share:
23,105
newbie
Author by

newbie

I'm a newbie, a novice, an amateur, a beginner in programming. My Total Programming Experience is approximately equal to the no. of months I joined here! So pardon me for my idiotic questions. My dream is to be a great programmer someday. To make the impossible, possible. I want to be the female version of Mr. Jon Skeet and my idol (who always scolds me hehehe) Mr Balusc. But I think, based on my current capabilities, i need a whooping 50 years to be on their level. So help me God! BTW, i really find this site super cool! I could interact and learn from many great programmers around the world. And for a programmer-wannabe like me, every opinion matters. What's important is that I'm learning a lot here which I could not learn by just reading books. So thank you everyone for all the help. I think I am becoming a stackoverflow addict.

Updated on March 07, 2020

Comments

  • newbie
    newbie over 4 years

    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..

  • KNU
    KNU about 10 years
    as per my finding if (resultSet!= null) always evaluates to true and (always)enters the if block . Don't think it's a good way to determine if resultset is empty.
  • Koray Tugay
    Koray Tugay about 10 years
    Javadoc says: "Retrieves the current row number. The first row is number 1, the second number 2, and so on."