怎样根据数据库的内容动态输出一个树形菜单(只要求能在控制台输出就可以了)   *****   
    数据库结构:       
  部门ID             部门名称         上级部门ID       
    1                 总部门              0       
    2                 部门1              1       
    3                 部门2              1       
    4                 部门3              1       
    5                 分部门1            2       
    6                 分部门2             2       
    7                 分部门3             2       
    8                 子部门1             5       
    9                 子部门1             5  

解决方案 »

  1.   

    你得自己定义一个javaBean,暂且取名为Node吧
    里面有4个属性,除了你表中的3个字段外,还应该加上一个List<Node>,也就是子节点集合遍历你的数据库,跟据上级部门ID把子节点全部加到List<Node>中,这样遍历结束后一个树形菜单就完了
      

  2.   

    LZ是操作数据库吗? ResultSet rs = ...;
     while(rs.hasNext())
     {
         Object o=rs.next();
         打印就对拉
     }
      

  3.   

    递归首先要找出边界。
    最简单的理解就是把一块大蛋糕切成N块.
    CREATE OR REPLACE PROCEDURE TREE_P(ID SCOTT.EMP.EMPNO%TYPE, GRADE NUMBER) AS
      CURSOR C IS SELECT * FROM SCOTT.EMP WHERE MGR = ID;
      PRE_STR VARCHAR2(1024) := '';
    BEGIN
       FOR i IN 1..GRADE LOOP
          PRE_STR := '  ' || PRE_STR;
       END LOOP;
       
       FOR curObj IN C LOOP
          DBMS_OUTPUT.PUT_LINE(PRE_STR || curObj.EMPNO);
          TREE_P(curObj.EMPNO, GRADE + 1);
       END LOOP;
    END;--入口
    CREATE OR REPLACE PROCEDURE TREE_TEST AS 
       CURSOR cur IS SELECT * FROM SCOTT.EMP WHERE MGR IS NULL;
    BEGIN
       FOR i IN cur LOOP
         DBMS_OUTPUT.PUT_LINE(i.EMPNO);
         TREE_P(i.EMPNO, 0);
       END LOOP;
    END;--调试时别忘了 SET SERVEROUTPUT ON;
      

  4.   

    测试结果。。SQL> set serveroutput on;
    SQL> exec TREE_TEST;
     
    7839
        7566
            7788
                7876
            7902
                7369
        7698
            7499
            7521
            7654
            7844
            7900
        7782
            7934
     
    PL/SQL procedure successfully completed
     
    SQL> 
      

  5.   

    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;public class TestRecursion
    {
    private ArrayList<Integer> idList = new ArrayList<Integer>();
    private ArrayList<String> nameList = new ArrayList<String>();
    private HashMap<Integer, Integer> indexList = new HashMap<Integer, Integer>();
    private HashMap<Integer, Integer> countList = new HashMap<Integer, Integer>(); public int listInit()
    {
    int first = 0;
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    String url = "";
    String user = "";
    String password = "";
    Connection con = DriverManager.getConnection(url, user, password);
    Statement stmt = con.createStatement(
      ResultSet.TYPE_SCROLL_INSENSITIVE,
      ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT 部门ID, 部门名称, 上级部门ID FROM 部门 ORDER BY 上级部门ID");
    int count = 1;
    int index = 0;
    int before = 0;
    while (!rs.isLast())
    {
    rs.next();

    if (rs.isFirst())
    {
    first = rs.getInt(2);
    } idList.add(rs.getInt(0));
    nameList.add(rs.getString(1));

    if (rs.getInt(2) == before)
    {
    count++;
    }
    else
    {
    indexList.put(rs.getInt(2), index);
    countList.put(before, count);
    count = 1;
    before = rs.getInt(2);
    }

    index++;
    }
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    return first;
    } public void recursion(String space, int parent)
    {
    String subSpace = space + " ";
    for (int i = indexList.get(parent).intValue(); i < countList.get(parent).intValue(); i++)
    {
    System.out.println(subSpace + nameList.get(i));
    if (indexList.containsKey(idList.get(i)))
    {
    recursion(subSpace, idList.get(i).intValue());
    }
    else
    {
    continue;
    }
    }
    } public static void main(String[] args)
    {
    TestRecursion tr = new TestRecursion();
    tr.recursion("", tr.listInit());
    }
    }写SQL查询语句的时候一定要用上级部门ID来排序,否则的话indexList(位置)和countList(个数)中的值不准确,也就不会得到正确的结果。
      

  6.   

    这个应该不是很难吧?定义一个类public class Department
    {
    private String name;
    private List<Department> subDparts;}然后依次递归就行了吧?
      

  7.   

    以前写过一个 无限极类别 的递归,下面你可以参考一下 public List digui(部门) {
    List b_list = new ArrayList();
    b_list.add(部门);

    String hql = "from 部门 b where b.上级部门.id=" + 部门.getId();
    Query query = session.createQuery(hql);
    List lst = query.list();

    for(int i=0; i<lst.size(); i++) {
    b_list.addAll(digui(lst.get(i)));
    }

    return b_list;
    }