站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题树形目录的递归实现数据库+jsp+javabean

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

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


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

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

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("用jsp种树");
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("

");
for(int i=0;i out.print(" ");
if(has_child(rs.getInt("id"))) {
out.print(" ");
out.print(""+rs.getString("message")+" id="+rs.getInt("id")+"");
out.println("");
} else
out.print(" "+rs.getString("message")+" id="+rs.getInt("id")+"");
out.println("
");
}
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;
}
}
%>

</td> </tr> <tr> <td vAlign=top align=left height="100%">
↑返回目录
前一篇: 我的通过eclipse来调试jsp的过程
后一篇: 导航树效果,JSP + JavaScript 实现