小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

樹(shù)形目錄的遞歸實(shí)現(xiàn)數(shù)據(jù)庫(kù)+jsp+javabean

 duduwolf 2005-09-28

樹(shù)形目錄的遞歸實(shí)現(xiàn)(一)數(shù)據(jù)庫(kù)+jsp+javabean

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!--
數(shù)據(jù)庫(kù)結(jié)構(gòu):
庫(kù)名: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
)
為了達(dá)到比較好的效果,這里準(zhǔn)備了五張小圖片
加號(hào):plus.gif
減號(hào):minus.gif
打開(kāi)的文件夾:openfold.gif
關(guān)閉的文件夾:closedfold.gif
白板:white.gif
-->
<%!//方便起見(jiàn)這里就不寫(xiě)成javabean了
class cn {//連接數(shù)據(jù)庫(kù),這里以MS-SQL為例
String jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//jdbc驅(qū)動(dòng)
String connectionString="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";//數(shù)據(jù)庫(kù)連接字符串
String user="sa";//數(shù)據(jù)庫(kù)用戶名
String pass="";//數(shù)據(jù)庫(kù)密碼
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種樹(shù)</title>");
 dowith(request);
 buildTree(out,0,0);//初始調(diào)用
}

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="white.gif"> ");
  if(has_child(rs.getInt("id"))) {
   out.print("<img alt="展開(kāi)" style="cursor:hand;" onclick="myClick(‘"+rs.getInt("id")+"‘);" id="img"+rs.getInt("id")+"" src="plus.gif"> <img id="im"+rs.getInt("id")+"" src="closedfold.gif"> ");
   out.print("<span onclick="myClick1(‘"+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);//遞歸調(diào)用
   out.println("</div>");
  } else
   out.print("<img src="minus.gif"> <img src="openfold.gif"> <span onclick="myClick1(‘"+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;
}
}
%>
<!--以上代碼可以寫(xiě)成javabean-->
<script language="JavaScript"><!--這段js為了實(shí)現(xiàn)樹(shù)的展開(kāi)和關(guān)閉的效果-->
<!--
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"?"minus.gif":"plus.gif";
 im.src=div.style.display!="none"?"openfold.gif":"closedfold.gif";
 img.alt=div.style.display!="none"?"關(guān)閉":"展開(kāi)";
}
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-表示根節(jié)點(diǎn)
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>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多