国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

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

2020-07-27 21:49:05
字體:
來源:轉載
供稿:網友

實現論壇樹型結構的算法很多,具體你可以去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()); 

%> 




算法參考:http://www.chinaasp.com/sqlbbs/showEssence.asp?id=4783 
上一篇:jsp搜索引擎

下一篇:jsp實現cookie的使用

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长汀县| 怀集县| 桃源县| 洛浦县| 固镇县| 高尔夫| 辽宁省| 漳平市| 西充县| 株洲市| 马公市| 宁蒗| 咸阳市| 鄂伦春自治旗| 汉川市| 卫辉市| 临汾市| 都江堰市| 泰安市| 游戏| 金乡县| 彭泽县| 阿拉善左旗| 合肥市| 黔江区| 林西县| 宿迁市| 濮阳县| 宜城市| 唐河县| 萝北县| 饶平县| 英德市| 抚顺县| 六盘水市| 平邑县| 砚山县| 马山县| 贞丰县| 腾冲县| 德兴市|