/*  * 创建日期 2005-4-14  *  * TODO 要更改此生成的文件的模板,请转至  * 窗口 - 首选项 - Java - 代码样式 - 代码模板  */ package treeMenu; import java.util.*; import java.sql.*; import dbmanager.DBConnection; import javax.servlet.jsp.JspWriter; /**  * @author 呆猴 lucky  *  * TODO 要更改此生成的类型注释的模板,请转至  * 窗口 - 首选项 - Java - 代码样式 - 代码模板  */ public class Tree {     private DBConnection conn;     public ArrayList arrayid;//定义包含所有id的数组     public ArrayList arrayname;//定义包含所有名称的数组     public ArrayList arrayparent;//定义包含所有父id的数组     public ArrayList class1_id;//定义包含所有一级信息id的数组     public Tree(){     try{       conn=new DBConnection();     }     catch(Exception e){       System.out.println("sorry");     }     }     /**      *       * 定义读取所有相关记录和一级信息的方法      */     public void buidTreeinit() throws SQLException{     ResultSet rs=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);//把所有id信息赋值到arrayid数组中      arrayname.add(name);//把所有name信息赋值到arrayname数组中      arrayparent.add(parent_id);//把所有parent_id信息赋值到arrayparent数组中      /**       * 把所有的一级信息赋值到数组class1_id中       */      if(parent_id.equals("0"))       {       class1_id.add(id);       }     }     conn.free();     }     /**      * 开始定义树型结构的构造      * @param parentid      * @throws SQLException      */     public void buildTree(JspWriter out,String parentid,int j) throws Exception{         j++;    ArrayList tmplist=new ArrayList();//包含所有父id为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+"修改  ";       table2=table2+"注册下级菜单  ";       table2=table2+"  <del>";       table2=table2+"</td>";       table2=table2+"</tr>";    while(arrayparent.indexOf(parentid)!=-1)    {      String tmpname=(String)arrayname.get(arrayparent.indexOf(parentid));//获取所有父id为parent_id的记录的名称      String tmpid=(String)arrayid.get(arrayparent.indexOf(parentid));//获取该子信息的id,用于赋予下级子信息的父id      if(has_child(tmpid)){      out.print(table2.replaceAll("<name>",tmpname).replaceAll("<del>",""));      }      else{      out.print(table2.replaceAll("<name>",tmpname).replaceAll("<del>","删除"));       }      int tmp=arrayparent.indexOf(parentid);//获取参数parent_id所在位置      arrayparent.remove(tmp);//删除参数parent_id所在位置的parent_id      arrayid.remove(tmp);//删除参数parent_id所在位置的id      arrayname.remove(tmp);//删除参数parent_id所在位置name      if(has_child(tmpid))//如果该条信息有相关子信息重新执行buildTree方法        {             buildTree(out,tmpid,j);        }          }      }     /**      * 进行是否有子信息判断      * @param parentid      * @return      */     public boolean has_child(String parentid)     {      boolean bb=false;           if(arrayparent.indexOf(parentid)!=-1)      {              bb=true;            }      return bb;     }     /**      * 树型结构显示      * @param args      * @throws Exception      */     public void showTree(JspWriter out) throws Exception{      Tree aa=new Tree();         aa.buidTreeinit();         String table1="";         table1=table1+"<tr bgcolor=\"#CCCCCC\">";         table1=table1+"<td width=\"70%\" height=\"30\">  <name></td>";       table1=table1+"<td width=\"30%\" height=\"30\" align='center'>";       table1=table1+"修改  ";       table1=table1+"注册下级菜单  ";       table1=table1+"  <del>";       table1=table1+"</td>";         table1=table1+"</tr>";         for(int i=0;i<aa.class1_id.size();i++)         {          if(aa.has_child((String)aa.class1_id.get(i))){          out.print(table1.replaceAll("<name>",(String)aa.arrayname.get(i)).replaceAll("<del>",""));          }          else{          out.print(table1.replaceAll("<name>",(String)aa.arrayname.get(i)).replaceAll("<del>","删除"));           }          aa.buildTree(out,(String)aa.class1_id.get(i),0);         }     } }
  数据库连接和数据库操作类: /*  * 创建日期 2005-4-14  *  * TODO 要更改此生成的文件的模板,请转至  * 窗口 - 首选项 - Java - 代码样式 - 代码模板  */ package dbmanager; import java.sql.*; /**  * @author Administrator  *  * TODO 要更改此生成的类型注释的模板,请转至  * 窗口 - 首选项 - Java - 代码样式 - 代码模板  */ public class DBConnection {  String user="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);   }   catch(Exception e){       System.err.println(e.getMessage());   }  }  //执行数据库查询  public ResultSet runRs(String sql){  rs=null;  try{  rs=stmt1.executeQuery(sql);  }  catch(SQLException ex){  System.err.println(ex.getMessage());  }  return rs;  }  //执行数据库其他操作  public  boolean  executeSql(String sql)  {  boolean che=false;  try  {  stmt.executeUpdate(sql);  che=true;  }  catch (SQLException ex)  {  System.err.println(ex.getMessage());  che=false;  }  return che;  }  //进行数据库连接释放  public boolean free(){  try {   connect.close();   if (stmt!=null){    stmt.close();       }   if(stmt1!=null){        stmt1.close();       }   return true;   }   catch (Exception e) {   return false;   }  } }
   
 
  |