首页 > 编程 > JSP > 正文

树形目录的递归实现数据库+jsp+javabean

2024-09-05 00:20:13
字体:
来源:转载
供稿:网友

树形目录的递归实现(一)数据库+jsp+javabean

<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<!--
数据库结构:
库名:test
表名:tree
create table [dbo].[tree] (
 [id] [int] identity (1, 1) not null ,
 [parentid] [int] not null ,
 [message] [varchar] (50) collate chinese_prc_ci_as null
)
为了达到比较好的效果,这里准备了五张小图片
加号:http://www.VeVb.com/htmldata/2005-09-27/plus.gif
减号:http://www.VeVb.com/htmldata/2005-09-27/minus.gif
打开的文件夹:http://www.VeVb.com/htmldata/2005-09-27/openfold.gif
关闭的文件夹:http://www.VeVb.com/htmldata/2005-09-27/closedfold.gif
白板:http://www.VeVb.com/htmldata/2005-09-27/white.gif
-->
<%!//方便起见这里就不写成javabean了
class cn {//连接数据库,这里以ms-sql为例
string jdbcdriver="com.microsoft.jdbc.sqlserver.sqlserverdriver";//jdbc驱动
string connectionstring="jdbc:microsoft:sqlserver://localhost:1433;databasename=test";//数据库连接字符串
string user="sa";//数据库用户名
string pass="";//数据库密码
connection conn=null;
resultset rs=null;

public cn() {
try {
 class.forname(jdbcdriver);
} catch(classnotfoundexception e) {
 system.err.println(e.tostring());
}
}

public resultset executequery(string sql) {
rs=null;
try {
conn=drivermanager.getconnection(connectionstring,user,pass);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
rs=stmt.executequery(sql);
}
catch(sqlexception e) {
system.err.println(e.tostring());
}
return rs;
}

public void executeupdate(string sql) {
try {
conn=drivermanager.getconnection(connectionstring,user,pass);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
stmt.executeupdate(sql);
}
catch(sqlexception e) {
system.err.println(e.tostring());
}
}
}

class tree {
cn conn=new cn();
public void init(javax.servlet.jsp.jspwriter out,javax.servlet.http.httpservletrequest request) throws exception {
 out.println("<title>用jsp种树</title>");
 dowith(request);
 buildtree(out,0,0);//初始调用
}

private void dowith(javax.servlet.http.httpservletrequest request) {
if(request.getparameter("parentid")==null||request.getparameter("parentid").equals(""))return;
string action=request.getparameter("action");
if(action.equals("add"))
 conn.executeupdate("insert into tree(parentid,message) values('"+request.getparameter("parentid")+"','"+request.getparameter("message")+"')");
else if(action.equals("delete"))
 conn.executeupdate("delete from tree where id="+request.getparameter("parentid")+" or parentid="+request.getparameter("parentid"));
}

public void buildtree(javax.servlet.jsp.jspwriter out,int parentid,int level) throws exception {
 level++;
 resultset rs=conn.executequery("select * from tree where parentid="+parentid+" order by id");
 while(rs.next()) {
  out.println("<div>");
  for(int i=0;i<level-1;i++)
   out.print("<img src="http://www.VeVb.com/htmldata/2005-09-27/white.gif"> ");
  if(has_child(rs.getint("id"))) {
   out.print("<img alt="展开" +rs.getint("id")+"');" id="img"+rs.getint("id")+"" src="http://www.VeVb.com/htmldata/2005-09-27/plus.gif"> <img id="im"+rs.getint("id")+"" src="http://www.VeVb.com/htmldata/2005-09-27/closedfold.gif"> ");
   out.print("<span +rs.getint("id")+"');" id="span"+rs.getint("id")+"">"+rs.getstring("message")+" id="+rs.getint("id")+"</span>");
   out.println("<div id="div"+rs.getint("id")+"">");
   buildtree(out,rs.getint("id"),level);//递归调用
   out.println("</div>");
  } else
   out.print("<img src="http://www.VeVb.com/htmldata/2005-09-27/minus.gif"> <img src="http://www.VeVb.com/htmldata/2005-09-27/openfold.gif"> <span +rs.getint("id")+"');" id="span"+rs.getint("id")+"">"+rs.getstring("message")+" id="+rs.getint("id")+"</span>");
  out.println("</div>");
 }
 rs.close();
 rs=null;
}

private boolean has_child(int parentid) throws exception {
 resultset rs=conn.executequery("select * from tree where parentid="+parentid+" order by id");
 return rs.next();
}

public string getoption() throws exception {
 string option="";
 resultset rs=conn.executequery("select * from tree order by id");
 while(rs.next())
  option+="<option value=""+rs.getint("id")+"">"+rs.getint("id")+"</option> ";
 return option;
}
}
%>
<!--以上代码可以写成javabean-->
<script language="javascript"><!--这段js为了实现树的展开和关闭的效果-->
<!--
function myclick(id) {
 eval("var div=div"+id);
 eval("var img=img"+id);
 eval("var im=im"+id);
 div.style.display=div.style.display!="none"?"none":"block";
 img.src=div.style.display!="none"?"http://www.VeVb.com/htmldata/2005-09-27/minus.gif":"http://www.VeVb.com/htmldata/2005-09-27/plus.gif";
 im.src=div.style.display!="none"?"http://www.VeVb.com/htmldata/2005-09-27/openfold.gif":"http://www.VeVb.com/htmldata/2005-09-27/closedfold.gif";
 img.alt=div.style.display!="none"?"关闭":"展开";
}
function myclick1(id) {
 document.form1.parentid.value=id;
}
//-->
</script>
<table>
<tr><td height="300" valign="top">
<%
tree mytree=new tree();
mytree.init(out,request);
%>
</td></tr>
<tr><td valign="top">
<%
if(mytree.getoption().equals("")) {
%>
<form name="form1" action="" method="get">
parentid:0-表示根节点
message:<input type="text" name="message">
<input type="hidden" name="parentid" value="0">
<input type="submit" name="action" value="add">
</form>
<%} else {%>
<form name="form1" action="" method="get">
parentid:<select name="parentid"><%=mytree.getoption()%></select>
message:<input type="text" name="message">
<input type="submit" name="action" value="add">&nbsp;<input type="submit" name="action" value="delete">
</form>
<%}%>
</td></tr></table>

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表