rs.next()返回值的奇怪问题?
我想实现select数据库里面的记录如果为null返回qqq,如果有记录择显示数据。
判断的时候检测rs1.next()是否为真可以区别是否存在记录。可是为什么当有数据时只显示一条?
try{
con=DBConnection.getConnection();
Statement stmt=con.createStatement();
ResultSet rs1=stmt.executeQuery( "select * from users where department= ' "+session.getAttribute( "department ").toString()+ " ' and duty= '5 ' and status= '0 ' ");
//System.out.print(rs1.next());
if(!rs1.next()){%>
<tr> <td> 提交对象: </td> <td colspan= "3 "> qqq </td> </tr>
<%}else{%>
<form action= "finish_send.jsp " name= " ">
<tr> <td> 提交对象: </td> <td colspan= "3 ">
<%while(rs1.next()){%>
<input type= "radio " name= "object " value= " <%=rs1.getString( "users_id ")%> "/> <%=rs1.getString( "realname ")%>
<%}%>
</td> </tr>
<tr> <td colspan= "4 " align= "center ">
<input type= "hidden " name= "app_id " value= " <%=session.getAttribute( "id ").toString()%> "/>
<input type=reset align= "middle " value= " 重 设 " class= "submit " size= "10 ">
<input type=submit value= " 提 交 " size= "10 " align= "middle " name= " " class= "submit "> </td> </tr>
</form>
<%}%>
<%
//关闭连接,释放资源
rs1.close();
stmt.close();
con.close();
}catch(SQLException ex){
ex.printStackTrace();
}%>
[解决办法]
少了一条
if(!rs1.next)
{
//你的写法对
}
else
{
//这里少了一条
out.print(rs1....输出一条)
再循环
while(rs1.next())
{}
}
[解决办法]
用 do{ ....... }while(rs1.next())