Specific Algorithm for implementing the Forum Tree Structure

Source: Internet
Author: User


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




Algorithm reference: http://www.chinaasp.com/sqlbbs/showEssence.asp? Id = 4783

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.