熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

實現論壇樹型結構的具體算法

2022-06-13   來源: JSP教程 

  作者flyblue
  實現論壇樹型結構的算法很多具體你可以去的全文搜索中查詢我現在的JSP論壇采用的也是當中的一種不用遞歸實現樹型結構的算法現在我將論壇樹型結構的具體算法和大家介紹一下和大家一起交流
  
  演示表的結構
  表名mybbslist
  字段
  數據類型
  說明
  BBSID 自動編號  
  RootID Int 根帖ID本身為根帖則RootID = ID
  FID Int 父帖ID上一層帖子的ID如是根帖則FID =
  DEPTH Int 根帖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程序
  
  <%@ page contentType="text/html;charset=gb2312" %>
  <%@ page import="java.io.*" %>
  <%@ page import="java.sql.*" %>
  <%
  int intRowCount;
  out.print("顯示論壇樹形結構");
  out.print("

");
  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("");
  while(j  {
  int rsDepth=rs.getInt("Depth");
  if (rsDepth  {
  for(int i=1;i  {
  out.print("");
  }
  }
  rsDepth=rs.getInt("Depth");
  if (rsDepth>Depth)
  {
  out.print("");
  }
  out.print("
");
  
  String bbssubject=rs.getString("bbssubject");
  out.print(bbssubject);
  out.print("");
  Depth = rs.getInt("Depth");
  j=j+1;
  rs.next();
  }
  out.print("");
  }
  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());
  }
  %>
  
  
  算法參考:?id=4783
  

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19792.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.