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

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

我有一个结果集。如果结果集为空或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>
  %>

isBeforeFirst()
isAfterLast()
这样的方法只需要用于可滚动的结果集。如果您希望能够使用所有类型的结果集和所有JDBC实现,那么您有几个选项:

  • 定义一个初始设置为false的布尔变量,并在
    while
    循环中将其设置为true;根据这个while循环做出决定
  • 类似于1,但定义一个整数计数
  • 选中
    rs.next()
    并使用
    do。。。而