最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

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

日期:11-17    来源:网页设计秀    作者:cnwebshow.com

  实现论坛树型结构的算法很多,我现在的jsp论坛采用的也是当中的一种:不用递归实现树型结构的算法,现在我将论坛树型结构的具体算法和大家介绍一下,和大家一起交流。Y4N中国设计秀

 Y4N中国设计秀

1、演示表的结构:Y4N中国设计秀

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

Y4N中国设计秀
2。创建表:Y4N中国设计秀

Y4N中国设计秀
create table mybbslist ( Y4N中国设计秀
 forumID int(20) not null, Y4N中国设计秀
 bbsID int auto_increment PRimary key, Y4N中国设计秀
 rootid int(20) not null, Y4N中国设计秀
 fid int(20) not null, Y4N中国设计秀
 depth int(20) not null, Y4N中国设计秀
 userID int(20) not null, Y4N中国设计秀
 bbsUser varchar(24) not null, Y4N中国设计秀
 bbsSubject varchar(100) not null, Y4N中国设计秀
 bbsContent text, Y4N中国设计秀
 bbsTime varchar(30), Y4N中国设计秀
 bbsRead int(20), Y4N中国设计秀
 bbsReply int(20), Y4N中国设计秀
INDEX forumID (forumID)) Y4N中国设计秀

3、连接MySQL数据库的BEANY4N中国设计秀

Y4N中国设计秀
package netzero; Y4N中国设计秀
import java.sql.*; Y4N中国设计秀
public class mydb Y4N中国设计秀
{ Y4N中国设计秀
String driverName = "org.gjt.mm.mysql.Driver"; Y4N中国设计秀
Connection conn = null; Y4N中国设计秀
Statement stmt = null; Y4N中国设计秀
ResultSet rs = null; Y4N中国设计秀
String connURL= "jdbc:mysql://localhost/mybbs?user=root&password=how&useUnicode=true&characterEncode=8859_1"; Y4N中国设计秀
//String connURL= "jdbc:mysql://localhost/netzerobbs?user=root&password=how"; Y4N中国设计秀
public mydb() Y4N中国设计秀
{ Y4N中国设计秀
try Y4N中国设计秀
{ Y4N中国设计秀
Class.forName(driverName); Y4N中国设计秀
} Y4N中国设计秀
catch (java.lang.ClassNotFoundException e) Y4N中国设计秀
{ Y4N中国设计秀
System.err.println("netzero(String): " + e.getMessage()); Y4N中国设计秀
} Y4N中国设计秀
}Y4N中国设计秀

Y4N中国设计秀
public ResultSet executeQuery(String sql) throws SQLException Y4N中国设计秀
{ Y4N中国设计秀
conn = DriverManager.getConnection(connURL); Y4N中国设计秀
stmt = conn.createStatement(); Y4N中国设计秀
rs = stmt.executeQuery(sql); Y4N中国设计秀
return rs; Y4N中国设计秀
}Y4N中国设计秀

Y4N中国设计秀
public boolean closeConn() Y4N中国设计秀
{ Y4N中国设计秀
try Y4N中国设计秀
{ Y4N中国设计秀
if (rs!=null) rs.close(); Y4N中国设计秀
if (stmt!=null) stmt.close(); Y4N中国设计秀
if (conn!=null) conn.close(); Y4N中国设计秀
return true; Y4N中国设计秀
} Y4N中国设计秀
catch ( SQLException ex ) Y4N中国设计秀
{ Y4N中国设计秀
System.err.println("closeConn: " + ex.getMessage()); Y4N中国设计秀
return false; Y4N中国设计秀
} Y4N中国设计秀
}Y4N中国设计秀

Y4N中国设计秀
} Y4N中国设计秀
 Y4N中国设计秀

4、显示论坛的JSP程序Y4N中国设计秀

Y4N中国设计秀
<jsp:useBean id="mybbs" scope="session" class="netzero.mydb" /> Y4N中国设计秀
<%@ page contentType="text/html;charset=gb2312" %> Y4N中国设计秀
<%@ page import="java.io.*" %> Y4N中国设计秀
<%@ page import="java.sql.*" %> Y4N中国设计秀
<% Y4N中国设计秀
int intRowCount; Y4N中国设计秀
out.print("显示论坛树形结构"); Y4N中国设计秀
out.print("<br><br>"); Y4N中国设计秀
try { Y4N中国设计秀
String sql="select * from mybbslist order by rootid desc,depth,fid,bbsid"; Y4N中国设计秀
ResultSet rs = mybbs.executeQuery(sql); Y4N中国设计秀
if (rs.next()) Y4N中国设计秀
{ Y4N中国设计秀
rs.last(); Y4N中国设计秀
intRowCount=rs.getRow(); Y4N中国设计秀
out.print("论坛树中有"); Y4N中国设计秀
out.print(intRowCount); Y4N中国设计秀
out.print("个叶子节点"); Y4N中国设计秀
rs.first(); Y4N中国设计秀
int j=0; Y4N中国设计秀
int Depth = 0; Y4N中国设计秀
out.print("<ul>"); Y4N中国设计秀
while(j<intRowCount) Y4N中国设计秀
{ Y4N中国设计秀
int rsDepth=rs.getInt("Depth"); Y4N中国设计秀
if (rsDepth<Depth) Y4N中国设计秀
{ Y4N中国设计秀
for(int i=1;i<Depth+1;i=i+1) Y4N中国设计秀
{ Y4N中国设计秀
out.print("</ul>"); Y4N中国设计秀
} Y4N中国设计秀
} Y4N中国设计秀
rsDepth=rs.getInt("Depth"); Y4N中国设计秀
if (rsDepth>Depth) Y4N中国设计秀
{ Y4N中国设计秀
out.print("<ul>"); Y4N中国设计秀
} Y4N中国设计秀
out.print("<li>");Y4N中国设计秀

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

本文引用地址:/bc/article_46740.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明