Search code examples
javahtmlsqljspreporting

ResultSet If Null Statement


I am trying to create an HTML Report that will print out the different line items. If the item is null, then I want it to skip the item, but I want to code to be able to handle the item when it is not null. My code currently is giving me an illegal expression error on my if statement. I think I have some kind of variable type issue.

<tr><td>
<%= if(rs.getString("delivery0delivery_address_2") != null){

      rs.getString("delivery0delivery_address_2")

     }
 else{

     %>
     <br>
     <%=
      }
      %>

Solution

  • First point worth noting is that <%= ... %> is not same as <% ... %>.

    You cannot use JSP expressions as you're using now.

    Either drop the = before the if clause and use simple JSP scriplet tag, otherwise if you're using JSP expressions, then use it like this:

    <tr><td>
    <%= (rs.getString("delivery0delivery_address_2") != null) ? rs.getString("delivery0delivery_address_2") : "<br>"%>
    

    You can visit JSP - what's the difference between “<% … %>” VS “<%= … %>” to clearly understand the difference between both JSP tags.