Recursive implementation of the tree directory (1) Database JSP JavaBean <% @ page contentty = "text / html; charset = GB2312" Language = "Java" IMPORT = "java.sql. *" ErrorPage = "%> < ! - Database Structure: Table Name: Test Table Name: TreeCreate Table [DBO]. [Tree] ([Id] [INT] Identity (1, 1) Not Null, [ParentID] [INT] NOT NULL, [MESSAGE] [varchar] (50) collate chinese_prc_ci_as null) In order to achieve a better effect, the five small pictures are prepared here: Plus.gif minus: Minus.gif Opened folder: OpenFold.gif Close folder: ClosedFold .gif white board: White.gif -> <%! // This is not written here in JavaBean Class CN {// Connecting Database, here is MS-SQL as an example String JDBCDriver = "com.microsoft.jdbc.sqlserver. SQLServerDriver "; // JDBC Drive String Connectionstring =" JDBC: Microsoft: SQLServer: // localhost: 1433; // Database connection string String user = "sa"; // database username String pass = " "; // Database password connection conn = null; resultset = null;
Public CN () {Try {class.Forname (jdbcdriver);} catch (classnotfoundexception e) {system.err.println (e.tostring ());}}
public ResultSet executeQuery (String sql) {rs = null; try {conn = DriverManager.getConnection (connectionString, user, pass); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery (sql ); Catch (SQLEXCEPTION E) {system.err.println (e.tostring ());} Return Rs;}
public void executeUpdate (String sql) {try {conn = DriverManager.getConnection (connectionString, user, pass); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate (sql);} catch (SQLException e) {system.err.println (e.tostring ());}}}
Class Tree {CN CONN = New CN (); public void init (javax.servlet.jsp.jspwriter out, javax.servlet.http.httpservletRequest request) throws exception {Out.println ("
public void buildTree (javax.servlet.jsp.JspWriter out, int parentid, int level) throws Exception {level ; ResultSet rs = conn.executeQuery ( "select * from tree where parentid =" parentid "order by id"); while (rs.next ()) {OUT.Println ("
ID = " rs.Getint (" ID ") " span> "); OUT.PRINTLN (" div> ");} r r cclose (); rs = null;} private boolean HAS_CHILD (int) Parentid) THROWS Exception {ResultSet RS = Conn.executeQuery ("Select * from trentid =" ParentID "ORDER BY ID"); return rs.next ();}
Public string getoption () throws exception {string Option = ""; ResultSet RS = conn.executeQuery ("SELECT * from tree order"); while (rs.next ()) Option = "