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

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

2022-06-13   來源: JSP教程 

  實現論壇樹型結構的算法很多具體你可以去wwwchinaaspcom的全文搜索中查詢我現在的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程序
 

  

  <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());
}
%>


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