实现论坛树型结构的算法很多,这里是一个不用递归实现树型结构的算法,原文来自网站《动态网站制作批南》http://www.knowsky.com/。站长整理。
这是运行示意图:
1、演示表的结构:
表名:mybbslist
字段 数据类型 说明
BBSID 自动编号
RootID Int 根帖ID,本身为根帖则RootID = BBSID
FID Int 父帖ID,上一层帖子的ID,如是根帖则FID = 0
DEPTH Int 根帖Level=0,其他依据回复的深度递增
BBSSubject Char 主题
2。创建表(可根据此表的结构在ACCESS中创建表):
create table mybbslist (
forumID int(20) not null,
bbsID int auto_increment primary key,
rootid int(20) not null,
fid int(20) not null,
depth int(20) not null,
userID int(20) not null,
bbsUser varchar(24) not null,
bbsSubject varchar(100) not null,
bbsContent text,
bbsTime varchar(30),
bbsRead int(20),
bbsReply int(20),
INDEX forumID (forumID))
3、演示论坛树型结构的JSP程序,注意此程序只对一个根贴进行了演示(数据库用ACCESS)
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String connURL= "jdbc:odbc:cwb";
Connection conn = null;
Statement stmt = null;
int intRowCount;
out.print("显示论坛树形结构");
out.print("<br><br>");
try {
Class.forName(driverName);
conn = DriverManager.getConnection(connURL);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql="select * from mybbslist order by rootid desc,depth,fid,bbsid";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()){
rs.last();
intRowCount=rs.getRow();
out.print("论坛树中有");
out.print(intRowCount);
out.print("个叶子节点");
rs.first();
int j=0;
int Depth = 0;
out.print("<ul>");
while(j<intRowCount){
int rsDepth=rs.getInt("depth");
if (rsDepth>Depth){
out.print("<ul>");
}
out.print("<li>");
String bbssubject=rs.getString("bbsSubject");
out.print(bbssubject);
out.print("</li>");
Depth=rsDepth;
j=j+1;
rs.next();
}
for(int i=0;i<Depth+1;i=i+1) {
out.print("</ul>");
}
}else{
out.print("数据库中无记录");
}
}catch (SQLException E) {
out.println("SQLException: " + E.getMessage());
out.println("SQLState: " + E.getSQLState());
out.println("VendorError: " + E.getErrorCode());
}
%>
<% //关闭mysql连接
try {
if(conn!=null) conn.close();
} catch (Exception ex) {
System.err.println("closeConn: " + ex.getMessage());
}
%>
posted on 2006-01-12 12:56
zjw_albert 阅读(108)
评论(0) 编辑 收藏