There are many algorithms for implementing the tree structure of the Forum. For details, you can go to www.chinaasp.com for full-text search. My current JSP Forum uses one of the following algorithms: Tree Structure algorithms are not implemented recursively. Now I will introduce the specific algorithms of the tree structure in the Forum and share them with you.
1. Demonstrate the table structure:
Table Name: mybbslist
Field data type description
Automatic bbsid ID
Rootid int root post ID. If it is the root post, it is rootid = ID.
FID int parent post ID, the ID of the previous post. If it is the root post, FID = 0
Depth int root post level = 0, others increase based on the depth of the reply
Bbssubject char topic
2. Create a table:
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. Bean connecting to the MySQL database
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.exe cutequery (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. display the JSP program of the Forum
<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 ("display Forum tree structure "); Out. Print ("<br> "); Try { String SQL = "select * From mybbslist order by rootid DESC, depth, FID, bbsid "; Resultset rs = mybbs.exe cutequery (SQL ); If (Rs. Next ()) { Rs. Last (); Introwcount = Rs. getrow (); Out. Print ("in the Forum Tree "); Out. Print (introwcount ); Out. Print ("leaf nodes "); 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 ("no record in Database "); } } Catch (sqlexception e ){ Out. println ("sqlexception:" + E. getmessage ()); Out. println ("sqlstate:" + E. getsqlstate ()); Out. println ("vendorerror:" + E. geterrorcode ()); } %> <% // Close the MySQL connection Try { If (! Mybbs. closeconn ()); } Catch (exception ex ){ System. Err. println ("closeconn:" + ex. getmessage ()); } %> |