and colleagues wrote an afternoon recursive read the JavaBean of the database
Source: Internet
Author: User
Recursion | data | database
/* * Date created 2005-4-14 * * TODO to change the template for this generated file, go to the * window-Preferences-Java-code style-code templates */package Treemenu; Import Java.util.*;import Java.sql.*;import Dbmanager. Dbconnection;import javax.servlet.jsp.jspwriter;/** * @author Monkey lucky * * TODO. To change the template for this generated type annotation, go to the * Window-Preferences-Java-code styles-code Templates */public class tree { private DbConnection conn; & nbsp The public ArrayList arrayid;//defines an array containing all IDs public ArrayList arrayname;//defines an array containing all the names The public ArrayList arrayparent;//defines an array containing all parent IDs Public ArrayList class1_id;//defines an array containing all the first-level information IDs public tree () { try{ conn=new dbconnection (); } catch (Exception e) { System.out.println ("Sorry"); } } /** * * Defines how to read all related records and first-level information &NBSP;&NBsp; */ public void Buidtreeinit () throws sqlexception{ ResultSet Conn.runrs ("SELECT * from Sp_sys_menu_item"); string Aa= ""; string id= ""; string Name= ""; string parent_id= ""; int i=0; Arrayid=new ArrayList (); arrayname=new ArrayList (); arrayparent=new ArrayList () ; class1_id=new ArrayList (); while (Rs.next ()) { id=rs.getstring ("id"); name= Rs.getstring ("name"); parent_id=rs.getstring ("parent_id"); Arrayid.add (ID);//Assign all ID information to Arrayid array arrayname.add (name);//Assign all name information to arrayname array arrayparent.add (parent_id)//Assign all parent_id information to arrayparent array /* * * Assign all the first-level information to the array class1_id */ if (Parent_id.equals ("0")) { class1_id.add (ID); } } Conn.free (); } /** * starts to define the construction of tree structure * @param parentid * @throws sqlexception */ & nbsp public void Buildtree (JspWriter out,string Parentid,int j) throws exception{ J++; arraylist tmplist=new ArrayList ();//The array of names containing all the records whose parent ID is parent_id String mmm= " "; string nnn= "|"; for (int q=0;q<j;q++) { nnn=nnn+ "--"; mmm=mmm+ " "; } String table2= ""; table2=table2+ "<tr bgcolor=\" #FFFFFF \ > "; table2=table2+" <td width=\ "70%\" Height=\ "30\" > "+mmm+nnn+" <name></td> "; table2=table2+" <td width= \ "30%\" height=\ "30\" align=\ "Center\" > "; table2=table2+" Modify "; table2=table2+ "Register downlevel menu "; table2=table2+ " <del> "; table2=table2+" </td> "; table2=table2+ "</tr>" while (Arrayparent.indexof (parentid)!=-1) { string tmpname= (String) arrayname.get (Arrayparent.indexof (ParentID)); Gets the name of all records with a parent ID of parent_id string tmpid= (String) Arrayid.get Arrayparent.indexof ( ParentID)//Gets the ID of this child information, the parent id if (Has_child (TMPID)) that assigns the subordinate child information { out.print (table2.replaceAll ("<name>", Tmpname). ReplaceAll ("<del>", ""); } else{ out.print (Table2.replaceall ("<name>", Tmpname). ReplaceAll ("<del>", "delete")); } int tmp= Arrayparent.indexof (ParentID)//Get parameter parent_id location arrayparent.remove (TMP); Deletes Parent_id arrayid.remove (TMP) at the location of the parameter parent_id;//delete parameter parent_id id arrayname.remove (TMP);//delete parameter parent_id location name if (has_ Child (Tmpid))//If the information has related sub information to Buildtree method { Buildtree (out,tmpid,j); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} } /** * Do you have any information to judge * @param parentid * @return */ Public Boolean has_child (String parentid) { boolean bb=false; if (Arrayparent.indexof (parentid)!=-1) {
database connections and Database operations classes:
* * Create Date 2005-4-14 * * TODO to change the template for this generated file, go to the * window-preferences-Java-code style-code Templates */package dbmanager;import java.sql.*;/** * @author Administrator * * TODO to change the template for this generated type annotation, go to the * window-preferences-Java-code style-code template */public class DbConnection {String use R= "SA"; String password= "SA"; String sdbdriver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"; String sconnstr= "Jdbc:microsoft:sqlserver://172.16.204.10:1433;databasename=hlsp_mis"; Connection Connect=null; ResultSet Rs=null; Statement stmt = null, STMT1 = NULL; Public DbConnection () {try{class.forname (sdbdriver); Connect = drivermanager.getconnection (Sconnstr,user,password); stmt = Connect.createstatement (); Stmt1=connect.createstatement (resultset.type_scroll_sensitive,resultset.concur_read_only);
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