Current location - Loan Platform Complete Network - Big data management - JSP dynamic tree menu with menu items from database
JSP dynamic tree menu with menu items from database
jsp dynamic tree menu shall be used to recursive algorithm, such as in the database there is a table, parent table, parent fields have id, name, depth, leve, ID incremental, depth is set to the number of levels, such as this data is the largest, 0, such as for the word menu for 1, and leve on the specified id of its parent node, to give the code of the paragraph you can explore the next! public Vector getModuleTree()

{

Vector pclass = new Vector();

try

{

stmt = con.createStatement();

String sql = "select * from Module where parentid = 0";

rs = stmt.executeQuery(sql);

Module cvo = null;

while(rs.next())

{

cvo = new Module();

cvo.setModule_id(rs.getInt("Module_id"));

cvo.setModule_name(rs.getString("Module_name"));

cvo.setModule_url(rs.getString("Module_url"));

cvo.setParentid(rs.getInt("parentid")); cvo.setRootid(rs.getInt("rootid")) ; cvo.setDepth(rs.getInt("depth")); pclass.add(cvo);

}

for (int i = 0; i < pclass.size(); i++)

{

Module pcvo = ( Module) pclass.get(i);

ShowTreeMenu(pcvo);

}

con.commit(); } catch (SQLException e)

{

e.printStackTrace() ;

} finally

{

try

{

if(rs!=null)

{

rs.close();

}

if(stmt!=null)

{

> stmt.close();

}

if(con!=null)

{

con.close();

}

}

catch (SQLException e)

{

e. printStackTrace();

}

}

}

return classList;

}

public void ShowTreeMenu(Module c)

{

Module ccvo = null;

String sql = "select * from Module where parentid = " + c.getModule_id();

Vector cclass = new Vector();

try

{

Module cvotemp;

stmt = con.createStatement();

rs = stmt.executeQuery(sql);

while(rs.next())

{

cvotemp = new Module();

cvotemp.setModule_id(rs.getInt("Module_id"));

cvotemp.setModule_name(rs.getString("Module_name"));

cvotemp.setModule_url(rs.getString("Module_url"));

cvotemp.setParentid(rs.getInt("parentid")); cvotemp.setRootid(rs.getInt(" rootid")); cvotemp.setDepth(rs.getInt("depth")); cclass.add(cvotemp);

}

System.out.println(cclass.size()+"(((((((((((((( (((((((((((9");

if (cclass.size() > 0)

{

c.setHasChild("have");

classList.add(c);

for (int j = 0; j < cclass.size(); j++)

{

ccvo = (Module) cclass.get(j);

ShowTreeMenu(ccvo);

} } else

{

classList.add(c) ;

}

} catch (SQLException e)

{

e.printStackTrace();

}

}

}