实现论坛树型结构的具体算法
程序员文章站
2023-12-15 16:53:16
实现论坛树型结构的算法很多,具体你可以去www.chinaasp.com的全文搜索中查询。我现在的jsp论坛...
实现论坛树型结构的算法很多,具体你可以去www.chinaasp.com的全文搜索中查询。我现在的jsp论坛采用的也是当中的一种:不用递归实现树型结构的算法,现在我将论坛树型结构的具体算法和大家介绍一下,和大家一起交流。
1。演示表的结构:
表名:mybbslist
字段
数据类型
说明
bbsid 自动编号
rootid int 根帖id,本身为根帖则rootid = id
fid int 父帖id,上一层帖子的id,如是根帖则fid = 0
depth int 根帖level=0,其他依据回复的深度递增
bbssubject char 主题
2。创建表:
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。连接mysql数据库的bean
package netzero;
import java.sql.*;
public class mydb
{
string drivername = "org.gjt.mm.mysql.driver";
connection conn = null;
statement stmt = null;
resultset rs = null;
string connurl= "jdbc:mysql://localhost/mybbs?user=root&password=how&useunicode=true&characterencode=8859_1";
//string connurl= "jdbc:mysql://localhost/netzerobbs?user=root&password=how";
public mydb()
{
try
{
class.forname(drivername);
}
catch (java.lang.classnotfoundexception e)
{
system.err.println("netzero(string): " + e.getmessage());
}
}
public resultset executequery(string sql) throws sqlexception
{
conn = drivermanager.getconnection(connurl);
stmt = conn.createstatement();
rs = stmt.executequery(sql);
return rs;
}
public boolean closeconn()
{
try
{
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
return true;
}
catch ( sqlexception ex )
{
system.err.println("closeconn: " + ex.getmessage());
return false;
}
}
}
4。显示论坛的jsp程序
<jsp:usebean id="mybbs" scope="session" class="netzero.mydb" />
<%@ page contenttype="text/html;charset=gb2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
int introwcount;
out.print("显示论坛树形结构");
out.print("<br><br>");
try {
string sql="select * from mybbslist order by rootid desc,depth,fid,bbsid";
resultset rs = mybbs.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)
{
for(int i=1;i<depth+1;i=i+1)
{
out.print("</ul>");
}
}
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 = rs.getint("depth");
j=j+1;
rs.next();
}
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(!mybbs.closeconn());
} catch (exception ex) {
system.err.println("closeconn: " + ex.getmessage());
}
%>
算法参考:http://www.chinaasp.com/sqlbbs/showessence.asp?id=4783