递归调用生成树型目录
<%@ 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
评论:0
阅读:568
引用:0
