樹形目錄的遞迴實現資料庫+jsp+javabean

來源:互聯網
上載者:User
js|遞迴|資料|資料庫

樹形目錄的遞迴實現(一)資料庫+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.webjx.com/htmldata/2005-09-27/plus.gif
減號:http://www.webjx.com/htmldata/2005-09-27/minus.gif
開啟的檔案夾:http://www.webjx.com/htmldata/2005-09-27/openfold.gif
關閉的檔案夾:http://www.webjx.com/htmldata/2005-09-27/closedfold.gif
白板:http://www.webjx.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.webjx.com/htmldata/2005-09-27/white.gif"> ");
  if(has_child(rs.getInt("id"))) {
   out.print("<img alt="展開" style="cursor:hand;" +rs.getInt("id")+"');" id="img"+rs.getInt("id")+"" src="http://www.webjx.com/htmldata/2005-09-27/plus.gif"> <img id="im"+rs.getInt("id")+"" src="http://www.webjx.com/htmldata/2005-09-27/closedfold.gif"> ");
   out.print("<span +rs.getInt("id")+"');" style="cursor:default;" id="span"+rs.getInt("id")+"">"+rs.getString("message")+" id="+rs.getInt("id")+"</span>");
   out.println("<div style="display:none;" id="div"+rs.getInt("id")+"">");
   buildTree(out,rs.getInt("id"),level);//遞迴調用
   out.println("</div>");
  } else
   out.print("<img src="http://www.webjx.com/htmldata/2005-09-27/minus.gif"> <img src="http://www.webjx.com/htmldata/2005-09-27/openfold.gif"> <span +rs.getInt("id")+"');" style="cursor:default;" 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.webjx.com/htmldata/2005-09-27/minus.gif":"http://www.webjx.com/htmldata/2005-09-27/plus.gif";
 im.src=div.style.display!="none"?"http://www.webjx.com/htmldata/2005-09-27/openfold.gif":"http://www.webjx.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"> <input type="submit" name="action" value="delete">
</form>
<%}%>
</td></tr></table>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.