JSP为什么只能循环出数据库里的第一条数据
- HTML code
<%@page pageEncoding="UTF-8"%><%@page import="java.sql.*,java.io.*,bbs.*,java.util.*"%><%!private void tree(List<Article> articles, Connection conn, int id, int grade) { String sql = "Select * from article where pid=" + id; Statement stmt = DB.creatStmt(conn); ResultSet rs = DB.executeQuery(stmt, sql); try { while (rs.next()) { Article a = new Article(); a.setId(rs.getInt("id")); a.setPid(rs.getInt("pid")); a.setRootId(rs.getInt("rootid")); a.setTitle(rs.getString("title")); a.setLeaf(rs.getInt("isLeaf") == 0 ? true : false); a.setPdate(rs.getTimestamp("pdate")); a.setGrade(grade); articles.add(a); if (!a.isLeaf()) { tree(articles, conn, a.getId(), grade + 1); } } } catch (SQLException e) { e.printStackTrace(); } }%><% List<Article> articles = new ArrayList<Article>(); Connection conn = DB.getConn(); tree(articles, conn, 0, 0); DB.close(conn);%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- saved from url=(0051)http://bbs.chinajavaworld.com/forum.jspa?forumID=20 --><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>论坛: Java语言*初级版</title> <link rel="stylesheet" type="text/css" href="./images/style.jsp"> </head> <body> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="1%"> <a href="http://bbs.chinajavaworld.com/index.jspa"><img src="./images/header-left.gif" width="199" height="38" border="0" alt="JAVA中文世界论坛|JAVA开发者论坛"> </a> </td> <td width="98%"> <img src="./images/header-stretch.gif" width="100%" height="38" border="0" alt=""> </td> <td width="1%"> <img src="./images/header-right.gif" width="5" height="38" border="0" alt=""> </td> </tr> </tbody> </table> <p class="jive-page-title"> 论坛: Java语言*初级版 </p> <p class="jive-description"> 探讨Java语言基础知识,基本语法等。大家一起交流,共同提高。 </p> <link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?forumID=20"> <script language="JavaScript" type="text/javascript" src="./images/common.js"></script> <div id="jive-forumpage"> <div class="jive-buttons"> <table summary="Buttons" cellpadding="0" cellspacing="0" border="0"> <tbody> <tr> <td class="jive-icon"> <a href="http://bbs.chinajavaworld.com/post!default.jspa?forumID=20"><img src="./images/post-16x16.gif" width="16" height="16" border="0" alt="发表新主题"> </a> </td> <td class="jive-icon-label"> <a id="jive-post-thread" href="http://bbs.chinajavaworld.com/post!default.jspa?forumID=20">发表新主题</a> </td> <td class="jive-icon"> </td> <td class="jive-icon-label"> </td> <td class="jive-icon"> </td> <td class="jive-icon-label"> </td> </tr> </tbody> </table> </div> <br> <input type="hidden" name="dateRange" value="last90days"> <table cellpadding="3" cellspacing="0" border="0" width="100%"> <tbody> <tr valign="top"> <td> <nobr></nobr> <span class="nobreak"> 页数: 1,482 - <span class="jive-paginator"> [ <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0&isBest=0" class="jive-current">1</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=25&isBest=0" class="">2</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0" class="">3</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=75&isBest=0" class="">4</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=100&isBest=0" class="">5</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=25&isBest=0">下一页</a> ] </span> </span> </td> </tr> </tbody> </table> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr valign="top"> <td width="99%"> <div class="jive-thread-list"> <div class="jive-table"> <table summary="List of threads" cellpadding="0" cellspacing="0" width="100%"> <thead> <tr> <th class="jive-first" colspan="3"> 主题 </th> <th class="jive-author"> <nobr> 作者 </nobr> </th> <th class="jive-view-count"> <nobr> 查看 </nobr> </th> <th class="jive-msg-count" nowrap=""> 回复 </th> <th class="jive-last" nowrap=""> 最后发表 </th> </tr> </thead> <tbody> <% for (Iterator<Article> it = articles.iterator(); it.hasNext();) { Article a = it.next(); String preStr = ""; for (int i = 0; i < a.getGrade(); i++) { preStr += " "; } %> <tr class="jive-odd"> <td width="1%" nowrap="" class="jive-first"> <div class="jive-bullet"> <img src="./images/read-16x16.gif" width="16" height="16" border="0" alt="读"> <!-- div--> </div> </td> <td width="1%" nowrap=""> </td> <td class="jive-thread-name" width="99%"> <img src="./images/ico_top.gif" align="absmiddle"> <img src="./images/ico_best.gif" align="absmiddle"> <a id="jive-thread-2" href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0"><%=preStr + a.getTitle()%></a> <span class="jive-thread-row-paginator"> <nobr> 页数: [ <a href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0&start=0">1</a> <a href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0&start=15">2</a> <a href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0&start=30">3</a> <a href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0&start=45">4</a> ... <a href="http://bbs.chinajavaworld.com/thread.jspa?threadID=737787&tstart=0&start=75">6</a> ] </nobr> </span> </td> <td class="jive-author" width="1%" nowrap=""> <span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=54029">Tiffany</a> </span> </td> <td class="jive-view-count" width="1%"> 10000 </td> <td class="jive-msg-count" width="1%"> 8888 </td> <td width="1%" nowrap="" class="jive-last"> <div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate())%> <br> 最后发表: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=1082980#1082980" title="Spring1988" style="">Tiffany</a> </div> </td> </tr> <% } %> </tbody> </table> </div> </div> <table cellpadding="3" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="99%"> </td> <td width="1%"> </td> </tr> </tbody> </table> <div class="jive-legend"></div> </td> </tr> </tbody> </table> </div> <br> </body></html>
---------------------------------------------
为什么做循环的时候,只能循环出数据库里的第一条数据,而不能全部循环出来呢。
[解决办法]
Select * from article where pid=" + id;
查出来的值有多少呢
[解决办法]
<%!private void tree(List<Article> articles, Connection conn, int id, int grade) {
String sql = "Select * from article where pid=" + id;
Statement stmt = DB.creatStmt(conn);
ResultSet rs = DB.executeQuery(stmt, sql);
try {
while (rs.next()) {
Article a = new Article();
a.setId(rs.getInt("id"));
a.setPid(rs.getInt("pid"));
a.setRootId(rs.getInt("rootid"));
a.setTitle(rs.getString("title"));
a.setLeaf(rs.getInt("isLeaf") == 0 ? true : false);
a.setPdate(rs.getTimestamp("pdate"));
a.setGrade(grade);
articles.add(a);
System.out.println("-------------" + articles.size() ) ;
if (!a.isLeaf()) {
tree(articles, conn, a.getId(), grade + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}%>
[解决办法]
- Java code
while (rs.next()) { Article a = new Article();//这里当你循环遍历的时候 你每次都创建新的实体、、、、改成循环外声明
[解决办法]
放在外面没有用。。你检查一下。。List里。。是否ADD进去了。。
[解决办法]
哪有这样的,这不越改越错[解决办法]
那个试试把参数List<Article> articles
在tree方法里重新定义成 articles = new ArrayList<Article>
行不行?
[解决办法]
你可以试试在sql那里打印下你组合起来的sql看看都查询了那些 我怀疑是
if (!a.isLeaf()) {
tree(articles, conn, a.getId(), grade + 1);
}
地方有问题
[解决办法]
首先看你pid为0的数据有几条
其次你pid为0的数据的isLeaf属性是不是false
在次判断你的pid为0的数据时不是有子记录
然后循环判断
你pid的为0的数据就以1条,而他的isLeaf属性又为true,或者就没有子记录,查询出来的当然是1条。
你自己可以这么样一步一步的看结果是什么,会走那条分支走,然后有没有数据添加进来,就知道你为啥是这个结果了啊