while(rs.next())
{
//这里是查回来的每一条记录
}

解决方案 »

  1.   

    可以设置一个int变量
    int i;
    while(rs.next())
    {
      i++;
    }
    如果i=0那就没有符合查询条件的记录
    rs返回一个ResultSet类型的值
      

  2.   

    if (rs == null){
         //无匹配记录
     }else{
         //有匹配的记录
     }
      

  3.   

    if(rs==null);//判断是否有记录
    select count(*) from table//可以用来查询数据记录的条数!
      

  4.   

    sboom(+-LingCh-+)(怪你过分美丽) 说的方法就行了,除非你的ResultSet rs=smt.executeQuery(sql);
    这个是调用的procedure,而且你的procedure里返回的是null,否则就用:
    while(rs.next())
    {
    //这里是查回来的每一条记录
    }
      

  5.   

    楼上你你可以借鉴一下啊 返回多个值用while  无记录的话你不需要别的判断的时候就不许要做别的操作 
      

  6.   

    楼上的各位兄弟还是不行呀?
    我的意思是这样的,我查询符合条件的记录,如果有就用表格的形式将它在这个页面都显示出来,如果没有一个符合条件的话就在这个页面只显示一句话做为提示。
    (rs==null)或者是(!rs)我都试了,还是不行,请问有其他的办法吗?
    兄弟刚学的,请各位大侠不吝赐教!谢谢!
      

  7.   

    先用select count(*) from where来判断有无匹配的记录用rs.wasNull()来判断
      

  8.   

    唉……
    看了这么多的回复,却没有满意的。。我写了两句,看看行否if(null!=rs){
    while(rs.next){
    //这里可以做你的取数据集的值。。
    }}
      

  9.   

    欧哟,大伙都说了这么多了,我就没有什么好补充的了!
    如果需要取值的话,请用rs.getString();
      

  10.   

    还是不行呀??????下面是我的源程序,请大家看一下
    <%@ page language="java" import = "java.sql.*,java.util.*"  contentType="text/html;charset=gb2312"%>
    <html>
    <head>
    <title>个人请假查询</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body background="image/bj.gif">
    <% try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:DNS1");
    Statement smt=con.createStatement();
    String getpersonID=(String)session.getAttribute("personID");
    String sql = "SELECT * FROM db WHERE (条件)";
    ResultSet rs=smt.executeQuery(sql);if(?????){
      con.close();
    %>
    <p align="center">您到目前为止还没有请假记录!</p>
    <p align="center"><a href="userBack.jsp">返回</a></p> 
    </body>
    <% } 
     else{
    %>
    <form id="S_form" name="S_form" method="post" action="" >
      <table border=1 align="center" bordercolor="#008000">
        <tr bgcolor="#99FFFF"> 
          <td width="10%" height="16"> 
            <p align="center">人员姓名</p></td>
          
    </tr>
      <%
      }
     con.close();
     %>
     </table>
    </form>
    <p align="center"><a href="userBack.jsp">返回</a></p> 
    </body>
    <% }
    }
    catch(Exception e){out.println(e.getMessage());}
    %>
    </html>
    if()中要填什么语句呀,用(rs==null)或(!rs.next())都不行,出现的问题是有这么几种:
    1)当没有记录的时候,不能显示我的那句提示话,而显示一个表头
    2)当有两条记录时就显示一条,当有一条时就只显示一个表头。
    这要怎么办?
    我的要求是:当没有任何记录时,就显示那句提示的话,和返回的连接,其余的都不显示(不能显示表头),
    当有记录是就显示表头和实际的记录条数都能显示出来。
    请大家帮一下呀 !感谢!
      

  11.   

    if(rs.next()){
    %><form id="S_form" name="S_form" method="post" action="" >
      <table border=1 align="center" bordercolor="#008000">
        <tr bgcolor="#99FFFF"> 
          <td width="10%" height="16"> 
            <p align="center">人员姓名</p></td>
        </tr>
    <% 
    do{
    %>
    <tr bgcolor="#99FFFF"> 
          <td width="10%" height="16"> 
            <p align="center"><%=rs.getString("你的人员姓名的字段名字")%></p></td>
        </tr>}while(rs.next());con.close();
     %>
     </table>
    </form>
    <p align="center"><a href="userBack.jsp">返回</a></p> 
    </body>}  else{ %>//endif
    <p align="center">您到目前为止还没有请假记录!</p>
    <p align="center"><a href="userBack.jsp">返回</a></p> 
    <%}%>//endelse
      

  12.   

    楼上的兄弟,谢谢,也谢谢所有帮我的兄弟姐妹。
    可是有一点,我不太明白就是我前面也是按照的你的这种方法,有一个地方不同,你用的是do{}while(),
    而我用的是while(){},如下:
    if(rs.next()){
    %><form id="S_form" name="S_form" method="post" action="" >
      <table border=1 align="center" bordercolor="#008000">
        <tr bgcolor="#99FFFF"> 
          <td width="10%" height="16"> 
            <p align="center">人员姓名</p></td>
        </tr>
    <% 
    while(rs.next()){
    %>
    <tr bgcolor="#99FFFF"> 
          <td width="10%" height="16"> 
            <p align="center"><%=rs.getString("你的人员姓名的字段名字")%></p></td>
        </tr>}con.close();
     %>
     </table>
    </form>
    <p align="center"><a href="userBack.jsp">返回</a></p> 
    </body>}  else{ %>//endif(省略)
    它出现这样的问题:如果没有记录能正确显示提示语句,但当只有一条记录时却只显示一个表头,有两条时才显示一条,三条就显示两条,这是为什么?????
    谢谢!
      

  13.   

    还有,即使是do{}while(条件)//表示先做再判断
    而while(条件){}先判断再做的话,当有多条记录时也是会丢掉最后一条记录,怎么会丢掉第一条记录呢?
      

  14.   

    欢迎光临http://www.nocoer.com网站!一个web开发人员汇集的地方,每天都有精彩的技术贴子和文章!欢迎大家讨论!
    每天都会有大量的供求信息发布!要做兼职的可以上来看看
    noco赌场!可以在这里赚取大量的cp点数
    noco考场!可以在这里证明你的开发能力
    noco道具市场!购买你需要的道具!让noco更有意思
    noco精灵市场!购买精灵为你作战!
    noco = note + code + er 
      

  15.   

    先看看javadoc文档怎么说的:
    next
    public boolean next()
                 throws SQLExceptionMoves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on. 
    If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read. 
    Returns:
    true if the new current row is valid; false if there are no more rows 
    Throws: 
    SQLException - if a database access error occurs那么你的错误在于:
    在if(判断)之后就已经指向第一个记录了(如果有的话),如果成功进入while,那么第一次循环的时候由于在while(判断)又一次调用next()函数,那么第一次打印的时候自然就会吧第一条记录给漏掉了。不知道这样表述你明白没有。
      

  16.   

    我看还是用这个吧:
    int i=0;
    while(rs.next())
    {
      i++;
    }
    if(i==0){
        //想做什么就做什么吧!
    }else{
        //想做什么就做什么吧!
    }