当前位置: 代码迷 >> Java Web开发 >> JSP为啥只能循环出数据库里的第一条数据
  详细解决方案

JSP为啥只能循环出数据库里的第一条数据

热度:2552   发布时间:2013-02-25 21:17:23.0
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">                                &nbsp;                            </td>                            <td class="jive-icon-label">                                &nbsp;                            </td>                            <td class="jive-icon">                                &nbsp;                            </td>                            <td class="jive-icon-label">                                &nbsp;                            </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>                                                        作者 &nbsp;                                                    </nobr>                                                </th>                                                <th class="jive-view-count">                                                    <nobr>                                                        查看 &nbsp;                                                    </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="">                                                    &nbsp; &nbsp;                                                </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%">                                            &nbsp;                                        </td>                                        <td width="1%">                                            &nbsp;                                        </td>                                    </tr>                                </tbody>                            </table>                            <div class="jive-legend"></div>                        </td>                    </tr>                </tbody>            </table>        </div>        <br>    </body></html>
  相关解决方案