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