日期:2014-05-17  浏览次数:20754 次

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" cells