Recursive implementation of tree-type directory database +jsp+javabean

Source: Internet
Author: User
Tags add eval sql return stmt table name tostring
js| Recursion | data | database

Recursive implementation of tree-type directory (i) database +jsp+javabean

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<!--
Database structure:
Library Name: Test
Table Name: Tree
CREATE TABLE [dbo]. [Tree] (
[ID] [int] IDENTITY (1, 1) not NULL,
[ParentID] [INT] Not NULL,
[Message] [varchar] (m) COLLATE chinese_prc_ci_as NULL
)
In order to achieve better results, there are five small pictures prepared
Plus: http://www.webjx.com/htmldata/2005-09-27/plus.gif
Minus sign: http://www.webjx.com/htmldata/2005-09-27/minus.gif
Open folder: Http://www.webjx.com/htmldata/2005-09-27/openfold.gif
Closed folder: Http://www.webjx.com/htmldata/2005-09-27/closedfold.gif
Whiteboard: Http://www.webjx.com/htmldata/2005-09-27/white.gif
-->
<%!//, it's not written javabean here.
Class CN {//Connection database, take Ms-sql for example
String jdbcdriver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver";//jdbc Drive
String connectionstring= "jdbc:microsoft:sqlserver://localhost:1433;databasename=test";//Database connection string
String user= "sa";//Database user name
String pass= "";//Database Password
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 seed Tree </title>");
Dowith (Request);
Buildtree (out,0,0);//Initial call
}

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 ("if (Has_child rs.getint ("id")) {
Out.print (" 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);//Recursive call
Out.println ("</div>");
} else
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>");
}
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;
}
}
%>
<!--above code can be written as javabean-->
<script language= "JavaScript" ><!--this JS in order to realize the effect of the tree's expansion and shutdown-->
<!--
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"? " Close ": unfold";
}
function MyClick1 (ID) {
Document.form1.parentid.value=id;
}
-->
</script>
<table>
&LT;TR&GT;&LT;TD height= "valign=" "Top" >
<%
Tree Mytree=new ();
Mytree.init (out,request);
%>
</td></tr>
&LT;TR&GT;&LT;TD valign= "Top" >
<%
if (Mytree.getoption (). Equals ("")) {
%>
<form name= "Form1" action= "method=" Get ">
parentid:0-represents the root node
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>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.