递归调用生成树型目录

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<%!
StringBuffer optionStr = new StringBuffer();
private void tree(Connection jdbc,int id,int level){
  ResultSet rs = null;
  Statement stmt = null;
  String preStr =
"";
  for(int i=0; i<level; i++) {
   preStr +=
"----";
  }
  String sql =
"select * from news_cat where PARENT_ID = " + id;
  try{
   stmt = jdbc.createStatement();
   rs = stmt.executeQuery(sql);
   while(rs.next()){
    optionStr.append(
"<option value=")    
       .append(rs.getString(
"cat_id"))
        .append(
">")
        .append(preStr)
       .append(rs.getString(
"cat_name"))
       .append(
"</option>");
    if(rs.getInt(
"is_leaf") != 0) {
     tree(jdbc, rs.getInt(
"cat_id"), level+1);
    }
   }
  } catch(SQLException e){
   e.printStackTrace();
  }  finally {
   try {
    if(rs != null) {
     rs.close();
     rs = null;
    }
    if(stmt != null){
      stmt.close();
      stmt = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
}
%>
<%
  Class.forName(
"oracle.jdbc.driver.OracleDriver");
  ResultSet rs = null;
  Statement stmt = null;
  Connection conn = null;
  conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:apple", "lunzi", "121561");
  stmt = conn.createStatement();
  rs = stmt.executeQuery(
"select * from news_cat where PARENT_ID = 0");
  
optionStr.append(
"<select name=\"news_cat\">");
while(rs.next()){
  optionStr.append(
"<option value=")
     .append(rs.getString(
"cat_id"))
     .append(
">")
     .append(rs.getString(
"cat_name"))
     .append(
"</option>");
    if(rs.getInt(
"is_leaf") != 0) {
      tree(conn,rs.getInt(
"cat_id"),1);
    }
}
  optionStr.append(
"</select>");
rs.close();
stmt.close();
conn.close();
out.println(optionStr.toString());
%>
lunzi   2007-05-06 02:50:52 评论:1   阅读:2564   引用:0
顶! @2009-08-21 03:15:24  Java Bug
太帅了...顶!

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog