首页后端开发其他后端知识实现论坛树型结构的具体算法

实现论坛树型结构的具体算法

时间2024-02-07 22:18:03发布访客分类其他后端知识浏览1012
导读:收集整理的这篇文章主要介绍了实现论坛树型结构的具体算法,觉得挺不错的,现在分享给大家,也给大家做个参考。 实现论坛树型结构的算...
收集整理的这篇文章主要介绍了实现论坛树型结构的具体算法,觉得挺不错的,现在分享给大家,也给大家做个参考。
实现论坛树型结构的算法很多,具体你可以去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(jintRowCount) 
{  
int rsDepth=rs.getInt("Depth");  
if (rsDepthDepth) 
{  
for(int i=1; iDepth+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 

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 实现论坛树型结构的具体算法
本文地址: https://pptw.com/jishu/604630.html
jsp搜索引擎 利用JSP建立Web站点

游客 回复需填写必要信息