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

树形目录的递归实现(一)数据库+jsp+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("用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;iout.print("

white.gif ");

if(has_child(rs.getInt("id"))) {

out.print("

plus.gif

closedfold.gif ");

out.print(""+rs.getString("message")+" id="+rs.getInt("id")+"");

out.println("");

buildTree(out,rs.getInt("id"),level);//递归调用

out.println("<>");

} else

out.print("

minus.gif

openfold.gif "+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+=""+rs.getInt("id")+" ";

return option;

}

}

%>tree myTree=new tree();

myTree.init(out,request);

%>

if(myTree.getOption().equals("")) {

%>

parentid:0-表示根节点

message:

parentid:

message:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值