您现在的位置:中国下载站学院中心网络编程JSP教程Jsp实例教程 → 文章列表

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

作者:佚名  来源:不详  发布时间:2006-12-29 11:21:48   

减小字体 增大字体

 
 
 实现论坛树型结构的算法很多,具体你可以去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());
}
%>

字体 】【itbulo.com/index.asp?boardid=51" title="如有错误,麻烦请及时告诉我们。谢谢。">报告错误】【itbulo.com/">进入软件交流区】【关闭

在百度中搜索更多JSP实现论坛树型结构的具体算法相关网页 转贴于:中国下载站

  • 上一篇文章:一个用JSP实现的分页的类及调用方法
  • 下一篇文章:在JSP页面中轻松实现数据饼图
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!