作者
實現論壇樹型結構的算法很多
表名
字段
數據類型
說明
BBSID 自動編號
RootID Int 根帖ID
FID Int 父帖ID
DEPTH Int 根帖Level=
BBSSubject Char 主題
create table mybbslist (
forumID int(
bbsID int auto_increment primary key
rootid int(
fid int(
depth int(
userID int(
bbsUser varchar(
bbsSubject varchar(
bbsContent text
bbsTime varchar(
bbsRead int(
bbsReply int(
INDEX forumID (forumID))
package netzero;
import java
public class mydb
{
String driverName =
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String connURL=
//String connURL=
public mydb()
{
try
{
Class
}
catch (java
{
System
}
}
public ResultSet executeQuery(String sql) throws SQLException
{
conn = DriverManager
stmt = conn
rs = stmt
return rs;
}
public boolean closeConn()
{
try
{
if (rs!=null) rs
if (stmt!=null) stmt
if (conn!=null) conn
return true;
}
catch ( SQLException ex )
{
System
return false;
}
}
}
<%@ 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