日期:2014-05-18  浏览次数:20587 次

java根据数据库展开树.
有谁 做过 java根据数据库展开树.
展示页面jsp
有好的例子没有,谢谢!

------解决方案--------------------
http://download.csdn.net/source/444826,lz可以下载这个树形结构,用sql语句将数据表里的内容放在相应的根节点和叶子节点上。
------解决方案--------------------
这是我前段时间做BBS的时候在网页中做的数据库树状结构展现的代码
<%!//这个是数据库展示为树状结构的方法
boolean login = false;//是否登陆了
String str = "";
String strLogin = "";
private void tree(Connection conn,int id,int level){
Statement stmt = null;
ResultSet rs = null;
String preStr = "";

for(int i=0;i<level;i++){
preStr = preStr + "----"; 
}

try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from article where pid =" + id);
while(rs.next()){
if(login){//如果登录进来了,显示删除链接
strLogin = "<a href='delete.jsp?id=" + rs.getInt("id") + "&pid=" + rs.getInt("pid") + "'>删除</a>";
}
str = str + "<tr><td>" + rs.getInt("id") + "</td><td>" + 
preStr + "<a href='showArticleDetail.jsp?id="+ rs.getInt("id") +"'>" + 
rs.getString("title") + "</a>" + 
"</td><td>" + strLogin +
"</td></tr>";
if(rs.getInt("isleaf") != 0){
tree(conn,rs.getInt("id"),level+1);
}
}
} catch(SQLException e){
e.printStackTrace();
} finally{
try{
if(stmt != null){
stmt.close();
stmt = null;
}
if(rs != null){
rs.close();
rs = null;
}
} catch(SQLException e){
e.printStackTrace();
}
}
}
%>

<%//在网页中调用方法
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=moon";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid = 0");

while(rs.next()){
if(login){//如果登录进来了,显示删除链接
strLogin = "<a href='delete.jsp?id=" + rs.getInt("id") + "&pid=" + rs.getInt("pid") + "'>删除</a>";
}
str = str + "<tr><td>" + rs.getInt("id") + "</td><td>" + 
"<a href='showArticleDetail.jsp?id="+ rs.getInt("id") +"'>" + rs.getString("title") + "</a>" + 
"</td><td>" + strLogin +
"</td></tr>";
if(rs.getInt("isleaf") != 0){
tree(conn,rs.getInt("id"),1);
}
}

rs.close();
stmt.close();
conn.close();
%>