问个问题(JSP)
rs1 ,rs2为查询两个表的记录集,以下是两个循环遍历:while(!rs1.afterLast())
{
code……
while(!rs2.afterLast())
{
code……
if(!rs2.afterLast())
{
rs2.next();
}
} if(!rs2.afterLast())
{
rs2.next();
}
}谁还有更好的遍历方法吗?例如不用afterLast()方法[问过很多人,都说没见过用afterLast()循环的,可能有问题],或用for来遍历,或其它,谢谢
----------------------------------其实是如下遇到的问题,抽象而来:
<% view.getcon();
String strTopic="select TopicName from Topic order by TopicID";
ResultSet RStopic=view.stmt.executeQuery(strTopic);
while(!RStopic.afterLast()) //外循环以遍历所有专题
{
%>
<tr>
<td colspan="6" bgcolor="#1E3A75" height="20">
<font color="White"><% =RStopic.getString("TopicName") %></font>
</td>
</tr>
<tr>
<td colspan="6" height="20"> </td>
</tr>
<% String strArtical="select * from Artical where TopicID="+RStopic.getString("TopicID")+"order by ArticalID";
ResultSet RSartical=view.stmt.executeQuery(strArtical);
while(RSartical.next()) //内循环以遍历专题内所有新闻
{
%>
<tr>
<td colspan="2" bgcolor="#FFFFFF" height="20"><div align="center"><font color="#FFFFFF"></font></div></td>
<td width="49" bgcolor="#B5C7EC" height="20"><div align="center"><font color="#FFFFFF"><%=RSartical.getString("ArticalID")%></font></div></td>
<td width="267" bgcolor="#D8E1F5"><div align="center"><font color="#FFFFFF"><%=RSartical.getString("Title")%></font></div></td>
<td width="56" bgcolor="#B5C7EC" height="20"><div align="center">编 </div></td>
<td width="80" bgcolor="#D8E1F5" height="20"><div align="center">删 </font></div> </td>
</tr>
<%
if(!RSartical.afterLast())
{
RSartical.next();
}
}
%>
<tr>
<td colspan="6" height="20"> </td>
</tr>
<%
RSartical.close();
if(!RStopic.afterLast())
{
RStopic.next();
}
}
RStopic.close();
view.close();
%>
请尽量提出见解
rs1 ,rs2为查询两个表的记录集,以下是两个循环遍历:while(!rs1.afterLast())
{
code……
while(!rs2.afterLast())
{
code……
if(!rs2.afterLast())
{
rs2.next();
}
} if(!rs2.afterLast())
{
rs2.next();
}
}谁还有更好的遍历方法吗?例如不用afterLast()方法[问过很多人,都说没见过用afterLast()循环的,可能有问题],或用for来遍历,或其它,谢谢
----------------------------------其实是如下遇到的问题,抽象而来:
<% view.getcon();
String strTopic="select TopicName from Topic order by TopicID";
ResultSet RStopic=view.stmt.executeQuery(strTopic);
while(!RStopic.afterLast()) //外循环以遍历所有专题
{
%>
<tr>
<td colspan="6" bgcolor="#1E3A75" height="20">
<font color="White"><% =RStopic.getString("TopicName") %></font>
</td>
</tr>
<tr>
<td colspan="6" height="20"> </td>
</tr>
<% String strArtical="select * from Artical where TopicID="+RStopic.getString("TopicID")+"order by ArticalID";
ResultSet RSartical=view.stmt.executeQuery(strArtical);
while(RSartical.next()) //内循环以遍历专题内所有新闻
{
%>
<tr>
<td colspan="2" bgcolor="#FFFFFF" height="20"><div align="center"><font color="#FFFFFF"></font></div></td>
<td width="49" bgcolor="#B5C7EC" height="20"><div align="center"><font color="#FFFFFF"><%=RSartical.getString("ArticalID")%></font></div></td>
<td width="267" bgcolor="#D8E1F5"><div align="center"><font color="#FFFFFF"><%=RSartical.getString("Title")%></font></div></td>
<td width="56" bgcolor="#B5C7EC" height="20"><div align="center">编 </div></td>
<td width="80" bgcolor="#D8E1F5" height="20"><div align="center">删 </font></div> </td>
</tr>
<%
if(!RSartical.afterLast())
{
RSartical.next();
}
}
%>
<tr>
<td colspan="6" height="20"> </td>
</tr>
<%
RSartical.close();
if(!RStopic.afterLast())
{
RStopic.next();
}
}
RStopic.close();
view.close();
%>
请尽量提出见解
List get专题();
List getNews(int 专题ID)
然后用一个for循环来完成List 专题s = get专题();
for(Iterator iter = 专题s.iterator; iter.hasNext();){
专题类 专题 = (专题类)iter.next();
List news = getNews(专题.getID());
显示专题和新闻;
}尽量不要在页面中写数据库相关代码。大概看了一眼,你是在finally中关闭的连结吗?如果不是,时间一长就会内存溢出了。
Moves the cursor to the end of this ResultSet object, just after the last row. This method has no effect if the result set contains no rows. 你的代码while(!rs1.afterLast())错了吧?afterLast()的功能是跳到结果集的最后一行,并且返回类型是void的!标准写法是:
while (rs.next()) {
//取数据处理
}