免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7884 | 回复: 4
打印 上一主题 下一主题

实现论坛树型结构的具体算法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2001-11-20 10:18 |只看该作者 |倒序浏览
作者:flyblue

实现论坛树型结构的算法很多,具体你可以去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=&quot;mybbs&quot; scope=&quot;session&quot; class=&quot;netzero.mydb&quot; />;
<%@ page contentType=&quot;text/html;charset=gb2312&quot; %>;
<%@ page import=&quot;java.io.*&quot; %>;
<%@ page import=&quot;java.sql.*&quot; %>;
<%
int intRowCount;
out.print(&quot;显示论坛树形结构&quot;
out.print(&quot;<br>;<br>;&quot;
try {
String sql=&quot;select * from mybbslist order by rootid desc,depth,fid,bbsid&quot;;
ResultSet rs = mybbs.executeQuery(sql);
if (rs.next())
{
rs.last();
intRowCount=rs.getRow();
out.print(&quot;论坛树中有&quot;
out.print(intRowCount);
out.print(&quot;个叶子节点&quot;
rs.first();
int j=0;
int Depth = 0;
out.print(&quot;<ul>;&quot;
while(j<intRowCount)
{
int rsDepth=rs.getInt(&quot;Depth&quot;
if (rsDepth<Depth)
{
for(int i=1;i<Depth+1;i=i+1)
{
out.print(&quot;</ul>;&quot;
}
}
rsDepth=rs.getInt(&quot;Depth&quot;
if (rsDepth>;Depth)
{
out.print(&quot;<ul>;&quot;
}
out.print(&quot;<li>;&quot;

String bbssubject=rs.getString(&quot;bbssubject&quot;);
out.print(bbssubject);
out.print(&quot;</li>;&quot;);
Depth = rs.getInt(&quot;Depth&quot;);
j=j+1;
rs.next();
}
out.print(&quot;</ul>;&quot;);
}
else
{
out.print(&quot;数据库中无记录&quot;);
}
}catch (SQLException E) {
out.println(&quot;SQLException: &quot; + E.getMessage());
out.println(&quot;SQLState: &quot; + E.getSQLState());
out.println(&quot;VendorError: &quot; + E.getErrorCode());
}
%>;
<% //关闭mysql连接
try {
if(!mybbs.closeConn());
} catch (Exception ex) {
System.err.println(&quot;closeConn: &quot; + ex.getMessage());
}
%>;


算法参考:http://www.chinaasp.com/sqlbbs/showEssence.asp?id=4783


论坛徽章:
0
2 [报告]
发表于 2010-09-23 01:06 |只看该作者
比较有价值

论坛徽章:
0
3 [报告]
发表于 2010-09-24 22:09 |只看该作者
代理放到code标签中读起来会更方便一些

论坛徽章:
0
4 [报告]
发表于 2010-10-01 21:06 |只看该作者
有价值。

论坛徽章:
12
射手座
日期:2014-10-02 11:31:29程序设计版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-27 06:20:00程序设计版块每日发帖之星
日期:2016-05-27 06:20:00程序设计版块每日发帖之星
日期:2016-05-25 06:20:00每日论坛发贴之星
日期:2016-05-24 06:20:00程序设计版块每日发帖之星
日期:2016-05-24 06:20:0015-16赛季CBA联赛之深圳
日期:2016-05-23 15:33:59程序设计版块每日发帖之星
日期:2016-05-20 06:20:00程序设计版块每日发帖之星
日期:2016-04-26 06:20:00神斗士
日期:2015-12-03 09:27:3215-16赛季CBA联赛之八一
日期:2016-12-29 09:56:05
5 [报告]
发表于 2014-08-28 18:09 |只看该作者
代理放到code标签中有价值很多
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP