java根据数据库展开树. 有谁 做过 java根据数据库展开树.展示页面jsp有好的例子没有,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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();%> 利用JdbcTemplate怎么得到数据库中的表的集合 在Ext下拉框中放一个grid 要怎么做才行呢? 求...servlet中文帮助文档 Myeclipse下的jsf问题 如何用jsp实现对文件夹改名? 谁知道ChinaRen.com(校友录)里面的班级相册部分是怎么做的? 为何这head.jsp中的汉字全变成了乱码 jspsmartupload 上传文件有文件类型限制吗??高手进!! 请教,如何使回车键跳到下一个文本框中,还有利用键盘的上,下,左右来移动到其他的文本框中!! JSP菜鸟提问,如何在jsp中调用一个java类文件。 document.all 问题! 帮帮我!
<%!//这个是数据库展示为树状结构的方法
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();
%>