The concrete algorithm of implementing the tree structure of forum based on JSP

Source: Internet
Author: User
Tags driver manager getmessage stmt table name mysql database

Implementation of the Forum tree structure of the algorithm many, specific you can go to www.chinaasp.com full-text search query. My current JSP forum is also one of the following: Do not have to implement the tree structure of the algorithm, now I will be the forum tree structure of the specific algorithm and you introduce, and everyone to communicate.

1, the structure of the demo table:

Table Name: mybbslist
field Data type description
BBSID Automatic Numbering
Rootid Int Root Post ID, which itself is the root post Rootid = ID
FID INT parent Post ID, the ID of the previous post, if the root thread fid = 0
DEPTH Int Root Post level=0, other depending on the depth of the reply increment
Bbssubject Char Theme

2. To create a table:

CREATE TABLE Mybbslist (
ForumID int () NOT NULL,
BBSID int Auto_increment primary KEY,
Rootid int () NOT NULL,
FID int (.) NOT NULL,
depth int () NOT NULL,
UserID int () NOT NULL,
Bbsuser varchar () NOT NULL,
Bbssubject varchar (m) NOT NULL,
Bbscontent text,
Bbstime varchar (30),
Bbsread Int (20),
bbsreply Int (20),
INDEX ForumID (ForumID))

3, connect the MySQL database 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 = Driver Manager.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, display the Forum JSP program


<%@ page contenttype= text/html;charset=gb2312%>
<%@ page import=" java.io.* "%>
<%@ page Import = "java.sql.*"%>
<%
int introwcount;
Out.print ("Show Forum tree Structure");
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 ("In the Forum Tree");
Out.print (Introwcount);
Out.print ("leaf node");
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 Records in Database");
}
}catch (SQLException E) {
Out.println ("SQLException:" + e.getmessage ());
Out.println ("SQLState:" + e.getsqlstate ());
Out.println ("Vendorerror:" + e.geterrorcode ());
}
%>
<%//close MySQL connection
try {
if (!mybbs.closeconn ());
} catch (Exception ex) {
System.err.println ("closeconn:" + ex.getmessage ());
}
%>

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.