如何检查结果集是否为空 [英] how to check result set empty or not

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

问题描述

我有一个结果集.如果结果集为空或null,那么我想在str16中设置"int b = 0"的值.并且如果结果集不为空/查找某些内容,则执行查询.但是当结果集为空时,我遇到了一些问题,那么在str16中未设置值.我在我的项目中所做的是,登录后,如果他已经休假,我将在员工资料上显示休假总数.如果员工没有请假,则在个人资料上显示0.

i have a resultset. if result set is empty or null, then i want to set the value of "int b=0" in str16. And if the result set is not empty/find out something then execute the query. But i am facing some problem when the resultset is empty, then value is not setting in str16. What i did in my project is that after login i will show the total number of leave on employee profile if he took leave already. if employee did not take any leave then show 0 on the profile.

* checkleave.jsp *

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

但是我在员工登录时遇到问题,如果列为空则出现错误.这是empprofile.jsp.它将在登录后出现.我在empprofile.jsp中输入的这段代码

but i am getting problem when employee login if column is empty then getting error. this is empprofile.jsp.it will come after login. this code i have put in empprofile.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实现,那么您可以使用以下几种选择:

Methods like isBeforeFirst() and isAfterLast() are only required to work with scrollable result sets. If you want to be able to use all types of result sets and all JDBC implementations, then you have several options:

  1. 定义一个初始设置为false的布尔变量,并在while循环中将其设置为true;根据此while循环做出决定
  2. 类似于一个,但定义一个整数计数
  3. 检查rs.next()并使用do ... while
  4. 处理结果
  1. Define a boolean variable initially set to false, and set it to true in the while loop; base your decision on this while loop
  2. Similar to one, but define an integer count instead
  3. Check rs.next() and process the result using do ... while

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

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