1. Structure of the demo table in the JSP Forum:
- Table Name: mybbslist
- Field data type description
- Automatic BBSID ID
- RootID Int root post ID, which is the root postRootID= ID
- FID Int parent post ID, the ID of the previous post, if it is the root postFID=0
- DEPTHInt root postLevel=0, Others increase based on the depth of the reply
- BBSSubject Char topic
-
2. Create a table in the JSP Forum:
- 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. JSP Forum Implementation of BEAN for connecting to 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.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 JavaScript/"target =" _ blank "> JSP program of the JSP Forum
- <Jsp: useBeanId="Mybbs" Scope="Session" Class="Netzero. mydb"/>
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. io .*"%>
- <%@ PageImport="Java. SQL .*"%>
- <%
- Int intRowCount;
- Out. print ("display Forum tree structure ");
- Out. print ("<br> ");
- Try {
- StringSQL="Select * from mybbslist order by rootid desc, depth, fid, bbsid";
- ResultSetRs=Mybbs. ExecuteQuery (SQL );
- If (rs. next ())
- {
- Rs. last ();
- IntRowCount=Rs. GetRow ();
- Out. print ("in the Forum Tree ");
- Out. print (intRowCount );
- Out. print ("leaf nodes ");
- Rs. first ();
- IntJ=0;
- IntDepth=0;
- Out. print ("<ul> ");
- While (j <intRowCount)
- {
- IntRsRsDepth= Rs. getInt ("Depth ");
- If (rsDepth <Depth)
- {
- For (intI=1; I <Depth + 1;II= I + 1)
- {
- Out. print ("</ul> ");
- }
- }
- RsRsDepth= Rs. getInt ("Depth ");
- If (rsDepth> Depth)
- {
- Out. print ("<ul> ");
- }
- Out. print ("<li> ");
-
- StringBbssubject=Rs. GetString ("bbssubject ");
- Out. print (bbssubject );
- Out. print ("</li> ");
- Depth=Rs. GetInt ("Depth ");
- JJ= J + 1;
- Rs. next ();
- }
- Out. print ("</ul> ");
- }
- 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 ());
- }
- %>
The preceding figure shows the specific algorithm and tree structure of the JSP forum. We hope this will help you.
- A brief overview of embedded objects in JSP
- JSP tutorial basics-HTML form demonstration
- JSP tutorials-basic articles-simple homepage Creation
- JSP tutorial basics: Technical Features of JSP
- What is JSP and its strong weakness?