Specific algorithms for implementing the tree structure of JSP forums

Source: Internet
Author: User

1. Structure of the demo table in the JSP Forum:

 
 
  1. Table Name: mybbslist
  2. Field data type description
  3. Automatic BBSID ID
  4. RootID Int root post ID, which is the root postRootID= ID
  5. FID Int parent post ID, the ID of the previous post, if it is the root postFID=0 
  6. DEPTHInt root postLevel=0, Others increase based on the depth of the reply
  7. BBSSubject Char topic
  8.  

2. Create a table in the JSP Forum:

 
 
  1. create table mybbslist (  
  2.  forumID int(20) not null,  
  3.  bbsID int auto_increment primary key,  
  4.  rootid int(20) not null,  
  5.  fid int(20) not null,  
  6.  depth int(20) not null,  
  7.  userID int(20) not null,  
  8.  bbsUser varchar(24) not null,  
  9.  bbsSubject varchar(100) not null,  
  10.  bbsContent text,  
  11.  bbsTime varchar(30),  
  12.  bbsRead int(20),  
  13.  bbsReply int(20),  
  14. INDEX forumID (forumID))  
  15.  

3. JSP Forum Implementation of BEAN for connecting to MYSQL database

 
 
  1. package netzero;  
  2. import java.sql.*;  
  3. public class mydb  
  4. {  
  5. String driverName = "org.gjt.mm.mysql.Driver";  
  6. Connection conn = null;  
  7. Statement stmt = null;  
  8. ResultSet rs = null;  
  9. String connURL= "jdbc:mysql://localhost/mybbs?user=root&password=how&useUnicode=true&characterEncode=8859_1";  
  10. //String connURL= "jdbc:mysql://localhost/netzerobbs?user=root&password=how";  
  11. public mydb()  
  12. {  
  13. try  
  14. {  
  15. Class.forName(driverName);  
  16. }  
  17. catch (java.lang.ClassNotFoundException e)  
  18. {  
  19. System.err.println("netzero(String): " + e.getMessage());  
  20. }  
  21. }  
  22.  
  23. public ResultSet executeQuery(String sql) throws SQLException  
  24. {  
  25. conn = DriverManager.getConnection(connURL);  
  26. stmt = conn.createStatement();  
  27. rs = stmt.executeQuery(sql);  
  28. return rs;  
  29. }  
  30.  
  31. public boolean closeConn()  
  32. {  
  33. try  
  34. {  
  35. if (rs!=null) rs.close();  
  36. if (stmt!=null) stmt.close();  
  37. if (conn!=null) conn.close();  
  38. return true;  
  39. }  
  40. catch ( SQLException ex )  
  41. {  
  42. System.err.println("closeConn: " + ex.getMessage());  
  43. return false;  
  44. }  
  45. }  
  46.  
  47. }  
  48.  

4. display the JavaScript/"target =" _ blank "> JSP program of the JSP Forum

 
 
  1. <Jsp: useBeanId="Mybbs" Scope="Session" Class="Netzero. mydb"/>
  2. <%@ PageContentType="Text/html; charset = gb2312"%>
  3. <%@ PageImport="Java. io .*"%>
  4. <%@ PageImport="Java. SQL .*"%>
  5. <%
  6. Int intRowCount;
  7. Out. print ("display Forum tree structure ");
  8. Out. print ("<br> ");
  9. Try {
  10. StringSQL="Select * from mybbslist order by rootid desc, depth, fid, bbsid";
  11. ResultSetRs=Mybbs. ExecuteQuery (SQL );
  12. If (rs. next ())
  13. {
  14. Rs. last ();
  15. IntRowCount=Rs. GetRow ();
  16. Out. print ("in the Forum Tree ");
  17. Out. print (intRowCount );
  18. Out. print ("leaf nodes ");
  19. Rs. first ();
  20. IntJ=0;
  21. IntDepth=0;
  22. Out. print ("<ul> ");
  23. While (j <intRowCount)
  24. {
  25. IntRsRsDepth= Rs. getInt ("Depth ");
  26. If (rsDepth <Depth)
  27. {
  28. For (intI=1; I <Depth + 1;II= I + 1)
  29. {
  30. Out. print ("</ul> ");
  31. }
  32. }
  33. RsRsDepth= Rs. getInt ("Depth ");
  34. If (rsDepth> Depth)
  35. {
  36. Out. print ("<ul> ");
  37. }
  38. Out. print ("<li> ");
  39.  
  40. StringBbssubject=Rs. GetString ("bbssubject ");
  41. Out. print (bbssubject );
  42. Out. print ("</li> ");
  43. Depth=Rs. GetInt ("Depth ");
  44. JJ= J + 1;
  45. Rs. next ();
  46. }
  47. Out. print ("</ul> ");
  48. }
  49. Else
  50. {
  51. Out. print ("no record in Database ");
  52. }
  53. } Catch (SQLException E ){
  54. Out. println ("SQLException:" + E. getMessage ());
  55. Out. println ("SQLState:" + E. getSQLState ());
  56. Out. println ("VendorError:" + E. getErrorCode ());
  57. }
  58. %>
  59. <%// Close the mysql connection
  60. Try {
  61. If (! Mybbs. closeConn ());
  62. } Catch (Exception ex ){
  63. System. err. println ("closeConn:" + ex. getMessage ());
  64. }
  65. %>

The preceding figure shows the specific algorithm and tree structure of the JSP forum. We hope this will help you.

  1. A brief overview of embedded objects in JSP
  2. JSP tutorial basics-HTML form demonstration
  3. JSP tutorials-basic articles-simple homepage Creation
  4. JSP tutorial basics: Technical Features of JSP
  5. What is JSP and its strong weakness?

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.