日期:2014-05-19  浏览次数:20737 次

jsp对于查询结果的显示
rs是查询结果集,正确赋值了 i,j就是计数用的
while(rs.next()){
i++ ;
String question = rs.getString(1);//获取问题内容
questionid = rs.getInt(2); //获取问题的id号
%>
<table borderColor = #999999 cellSpacing = 0 cellPadding = 0 border = 0>
<tr bgcolor = #999999><td>&nbsp;<%=i%> . <%=question %></td></tr>
<%
//子查询语句,根据问题的id 号到Items 表中查询对应该问题的所有答案(item)
String ch_sql = "select Item,ItemId from Items where QuestionId="+questionid;
ch_rs = stmt.executeQuery(ch_sql);
j = 0;

while(ch_rs.next()){
j++;
String item = ch_rs.getString(1);
itemid = ch_rs.getInt(2);

%>
<tr><td><%=j%><input type="radio" value="<%=itemid%>" name="<%=questionid%>"><%=item%>
</td></tr>
<%
  }//end of select item
  %>
  </table><p>
  <%
}//end of select question

}catch(Exception e){
out.println("读取问题页面问题:"+e);
}
  现在问题是,我的查询后显示的结果只有一个,我的数据库中有好多个结果的本应该是循环的,好多的问题及结果才对,感觉这个地方的循环没有效果,怎么回事?


------解决方案--------------------
ResultSet 只能取一次。
如果要使用, 建议先循环一次读到List等容器中, 以后就只循环这个List就行了

http://topic.csdn.net/u/20080117/16/4d85c232-e454-4789-9d4d-87e472388623.html
------解决方案--------------------
查两次做什么,直接连接查询了一次循环迭代不行?

String ch_sql = "select i.Item,i.ItemId from Items i,主表 where QuestionId=主表.外键";

这个应该可以吧?