jsp

位置:IT落伍者 >> jsp >> 浏览文章

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


发布日期:2021年08月14日
 
JSP实现论坛树型结构的具体算法

实现论坛树型结构的算法很多具体你可以去wwwchinaaspcom的全文搜索中查询我现在的JSP论坛采用的也是当中的一种不用递归实现树型结构的算法现在我将论坛树型结构的具体算法和大家介绍一下和大家一起交流

演示表的结构

表名mybbslist
字段 数据类型说明
BBSID自动编号
RootID Int 根帖ID本身为根帖则RootID = ID
FIDInt 父帖ID上一层帖子的ID如是根帖则FID =
DEPTHInt 根帖Level=其他依据回复的深度递增
BBSSubject Char主题

创建表

create table mybbslist (
forumID int() not null
bbsID int auto_increment primary key
rootid int() not null
fid int() not null
depth int() not null
userID int() not null
bbsUser varchar() not null
bbsSubject varchar() not null
bbsContent text
bbsTime varchar()
bbsRead int()
bbsReply int()
INDEX forumID (forumID))

连接MYSQL数据库的BEAN

package netzero;
import javasql*;
public class mydb
{
String driverName = "orggjtmmmysqlDriver";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String connURL= "jdbc:mysql://localhost/mybbs?user=root&password=how&useUnicode=true&characterEncode=_";
//String connURL= "jdbc:mysql://localhost/netzerobbs?user=root&password=how";
public mydb()
{
try
{
ClassforName(driverName);
}
catch (javalangClassNotFoundException e)
{
Systemerrprintln("netzero(String): " + egetMessage());
}
}

public ResultSet executeQuery(String sql) throws SQLException
{
conn = DriverManagergetConnection(connURL);
stmt = conncreateStatement();
rs = stmtexecuteQuery(sql);
return rs;
}

public boolean closeConn()
{
try
{
if (rs!=null) rsclose();
if (stmt!=null) stmtclose();
if (conn!=null) connclose();
return true;
}
catch ( SQLException ex )
{
Systemerrprintln("closeConn: " + exgetMessage());
return false;
}
}

}

显示论坛的JSP程序

<jsp:useBean id="mybbs" scope="session" class="netzeromydb" />
<%@ page contentType="text/html;charset=gb" %>
<%@ page import="javaio*" %>
<%@ page import="javasql*" %>
<%
int intRowCount;
outprint("显示论坛树形结构");
outprint("<br><br>");
try {
String sql="select * from mybbslist order by rootid descdepthfidbbsid";
ResultSet rs = mybbsexecuteQuery(sql);
if (rsnext())
{
rslast();
intRowCount=rsgetRow();
outprint("论坛树中有");
outprint(intRowCount);
outprint("个叶子节点");
rsfirst();
int j=;
int Depth = ;
outprint("<ul>");
while(j<intRowCount)
{
int rsDepth=rsgetInt("Depth");
if (rsDepth<Depth)
{
for(int i=;i<Depth+;i=i+)
{
outprint("</ul>");
}
}
rsDepth=rsgetInt("Depth");
if (rsDepth>Depth)
{
outprint("<ul>");
}
outprint("<li>");

String bbssubject=rsgetString("bbssubject");
outprint(bbssubject);
outprint("</li>");
Depth = rsgetInt("Depth");
j=j+;
rsnext();
}
outprint("</ul>");
}
else
{
outprint("数据库中无记录");
}
}catch (SQLException E) {
outprintln("SQLException: " + EgetMessage());
outprintln("SQLState: " + EgetSQLState());
outprintln("VendorError: " + EgetErrorCode());
}
%>
<% //关闭mysql连接
try {
if(!mybbscloseConn());
} catch (Exception ex) {
Systemerrprintln("closeConn: " + exgetMessage());
}
%>

上一篇:JSP初学者必须掌握的语法点

下一篇:JSP语言令开发人员钟爱的五大优势