Remove a tree structure according to the database

zhaozj2021-02-17  52

In Servlet / JSP development, we may also involve the definition of tree structure, such as removing records from the database and defining an XML tree for display for the forum structure. Because it is a servlet, JTREE provided in Java Swing is not realistic.

In fact, a tree that realizes the basic function is not complicated, so we have been moving.

First, the structure of the database is usually such a few fields:

INT ID Node iDint PID Parent Node IDString Title Display String ...

(In fact, the first two nodes maintain the structure of the tree is the most important, and how do you want to add it ...)

Then we will define a tree node class, the code is as follows:

/ *** Customized tree knot * @Author: Sharetop (Sharetop@hotmail.com) * / import java.util. *;

Class CTREENODE {INT ID; / * Point ID number, corresponding database corresponding field * / string title = null; string href = null; ctreenode parent = null; / * Parent Node Object * / Vector AllChild; / * Save all child nodes * / / ** * Construction * / ctreenode () {allchild = new vector ();} / ** * added sub-node * / void addChild (ctreenode child) {allchild.add (child);}

/ ** * Recursively check the child node according to the id, return node object * / ctreenode findchild (int id) {if (id == this.id) Return this; for (enumeration E = allchild.ements (); E.hasMoreElements );) {Ctreenode temp = (ctreenode) E.NEXTELEMENT (); if (temp.id == id) Return Temp; CTREENODE TMP = Temp.FindChild (ID); if (tmp! = Null) Return TMP;} return NULL;} / ** * Generate an XML string * / string create () {stringbuffer result = new stringbuffer (); result.append (" "); if (allchild.size ()! = 0) {for (Enumection E = AllChild.Elements (); E.hasMoreElements (); {ctreenode temp = (ctreenode) E.NEXTELEMENT (); result.create ());}}}} result.append (""); Return RESULT .tostring ();

Ok, then we use this class in servlet:

Add a method in our testservlet.java:

Private string getXmlResult () {stringbuffer result = new stringbuffer (); / * Here is tree roots * / ctreenode root = new ctreenode (); root.id = 0; root.title = "root directory"; root.href = " "; Try {connection con = dbpool.getConnection (); preparedStatement PS = Con.PrepareStatement (" Select * from block order "); ResultSet RS = ps.executeQuery (); while (rs.next ()) {/ * Each record corresponds to a node, the key is to find it in the location * / ctreenode node = new ctreenode (); node.id = rs.getinT ("id"); node.title = gtstring "Title"; node.parent = root.findchild ("pid")); node.href = node.title ". HTML"; node.parent.addchild (node);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} rclose () Ps.close (); con.close (); dbpool.freeConnection (con);} catch (exception ex) {system.err.println ("Error In Update - Sqlbean: / R / N"); EXPRINTSTACKTRACE "Return" ";} return root.create ();} is as simple, TestServlet's Doget method I also give it to it

public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType ( "text / xml; charset = gb2312"); PrintWriter out = resp.getWriter (); out.println ( " "); Out.print (getResult ()); out.flush (); out.close ();}

Interested can test it.

This is just a basic tree structure. If you want it to meet your requirements, you need to expand it, welcome to discuss with me Sharetop@hotmail.com

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

New Post(0)