Jsp 如何检查结果集是否为空

Jsp 如何检查结果集是否为空,jsp,Jsp,我有一个结果集。如果结果集为空或null,那么我想在str16中设置“int b=0”的值。如果结果集不是空的,则执行查询。但是当resultset为空时,我遇到了一些问题,然后str16中没有设置值。我在我的项目中所做的是,登录后,如果员工已经休假,我将在员工档案中显示休假总数。如果员工没有休假,则在个人资料上显示0 checkleave.jsp <%@ page import="java.sql.*" %> <%@ page import="java.i

我有一个结果集。如果结果集为空或null,那么我想在str16中设置“int b=0”的值。如果结果集不是空的,则执行查询。但是当resultset为空时,我遇到了一些问题,然后str16中没有设置值。我在我的项目中所做的是,登录后,如果员工已经休假,我将在员工档案中显示休假总数。如果员工没有休假,则在个人资料上显示0

checkleave.jsp

     <%@ page import="java.sql.*" %> 
    <%@ page import="java.io.*" %> 
     <%
       // storing the login id in abc becouse i have give table name as employee id 
      String abc= session.getAttribute("empid").toString();
      %>
      <%    
        try{
     int b=0;         
            Statement st=connection.createStatement();  

     ResultSet rs=st.executeQuery("select approcode,approemail,appromon,approvemon,approtue,approvetue,approwed,approvewed,approthr,approvethr,approfri,approvefri,approsat,approvesat,commen,months,SUM(nol) from `pushkalit`.`approval`WHERE (CONVERT( `approcode` USING utf8 ) LIKE '%"+abc+"%')");// here what i am doing  that if employee login then check in approval table where is approcode column as employee id if find something then execute table if column is not there then put value of b(i have declared int b=0) in str16. 

   if(rs !=null)       
   {  
     if (rs.isBeforeFirst() && rs.isAfterLast())  
      {  
       session.setAttribute("str16",b);
        }
        else
       {       
         while(rs.next())
        {  
          String str=rs.getString("approcode");
          String str1= rs.getString("approemail");
          String str2=rs.getString("appromon");
            String str3=rs.getString("approvemon");
          String str4=rs.getString("approtue"); 
          String str5=rs.getString("approvetue");
           String str6=rs.getString("approwed"); 
              String str7=rs.getString("approvewed");
              String str8=rs.getString("approthr");
              String str9=rs.getString("approvethr");
               String str10=rs.getString("approfri");
              String str11=rs.getString("approvefri");
            String str12=rs.getString("approsat");
           String str13=rs.getString("approvesat");
              String str14=rs.getString("commen");
             String str15=rs.getString("months");
             String str16=rs.getString(17);
            session.setAttribute("str16",str16);    
        }      
    }
    }
    else
    {
    session.setAttribute("str16",b);
    }

}
catch(Exception e) {
    System.out.println(e);
}
 %>

但当员工登录时我遇到了问题,若列为空,那个么就会出现错误。 这是emprofile.jsp。它将在登录后出现。这段代码是我放在emprofile.jsp中的

<%
  String a = session.getAttribute("str16").toString();
 int y = Integer.parseInt(a);
 <tr><td><label>Total Leave Day:</label></td>
    <td><%=y %></td>
  %>

如果
rs.next()==false,则结果集为空。因此,在您的例子中,创建一个布尔值,将其设置在while(rs.next())循环中,并在循环后进行检查

boolean blnResultSetIsEmpty = true;
while(rs.next())
{
   blnResultSetIsEmpty = false;
   ...
}
if(blnResultSetIsEmpty)
{
   out.print("There were no results");
}
或者,由于您的代码显然只需要一行,请使用if语句而不是while循环:

if(rs.next())
{
   ...
}
else
{
   out.print("There were no results");
}

可能重复,但如果结果集为空,我必须不打印任何内容。如果结果集为空,我必须在会话变量中输入值0。这只是一般的想法。