Specific algorithm for implementing the Forum tree structure _ MySQL

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. Demo table structure: table name: mybbslist field data type
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

<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. io. *" %>
<% @ Page import = "java. SQL. *" %>
<%
Int intRowCount;
Out. print ("display Forum tree structure ");
Out. print ("

");
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 ("
    ");
    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 ("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

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.