JSP implementation of specific algorithms for the forum tree structure

xiaoxiao2021-03-06  37

1. Structure of the demo: Name: Mybbslist field data type Description BBSID Auto number rootid int root post ID, itself is the root post rootid = ID FID INT parent post = 0 Depth INT Renal Level = 0, other deep incremental BBSSUBJECT CHAR topics based on reply

2. Create a table:

Create Table Mybbslist (ForumID Int (20) Not Null, BBSID INTO_INCREMENT Primary Key, Rootid Int (20) Not Null, FID 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)

3, connect the bean in 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 (ConnUR); stmt = conn.createstatement (); rs = stmt.executeQuery (SQL); returnlean closeconn () {trylean 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 consttype = "text / html; charSet = GB2312"%> <% @ page import = "java.io . * "%> <% @ page import =" java.sql *. "%> <% int intRowCount; out.print (" show Forum tree "); out.print ("

" ); try {string sql = "select * from mybbslist Order by rootid desc, defth, fid, bbsid"; resultset = mybbs.executeQuery (sql); if (rs.next ()) {rl.as (); Introwcount = rs.getrow (); out.print ("Intum tree"); out.print (Introwcount); Out.print ("Int Leave Node"); rs.first (); int J = 0; int Depth = 0; OUT.PRINT ("

    "); while (j ");}}}}}}}}}}}}}}}}}} r rT ("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 ("There is no record in the database");}} catch (SQLEXCEPTION E) {OUT.PRINTLN ("SQLEXCEption : " E.getMessage ()); Out.println (" SqlState: " E.GETSQLSTATE ()); OUT.PRINTLN (" Vendorerror: " E.GetersRrorcode ());}%> <% // Close MySQL connection TRY {if (! mybbs.closeconn ());} catch (exception ex) {system.err.println ("

转载请注明原文地址:https://www.9cbs.com/read-69275.html

New Post(0)