再说递归...
===ShowTree.jsp=== <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.sql.*" %> <% Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:orcl","scott","tiger"); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from article where pid=0"); %> <%! static String preout=""; private static void getTree(Connection conn,int id,int level) { Statement st=null; ResultSet rs=null; String blk=""; String que="select * from article where pid="+id; for(int i=0;i<level;i++) { blk=blk+"---"; } try { st=conn.createStatement(); rs=st.executeQuery(que); while(rs.next()) { preout+="<tr><td>"+blk+rs.getInt("id")+"</td><td>"+"<a href='show article deatil.jsp?id="+rs.getInt("id")+"'>"+rs.getString("cont")+"</a>"+"</td></tr>"; if(rs.getInt("isLeaf")!=1) { getTree(conn,rs.getInt("id"),level+1); } } } catch(SQLException e) { e.printStackTrace(); } } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <hr> <table border="1"> <% rs.next(); %> <tr><td><%=rs.getInt("id")%></td><td><%= rs.getString("cont")%></td></tr> <% getTree(conn,0,1); out.print(preout); %> </table>> </body> <% rs.close(); st.close(); conn.close(); %> </html>
递归算法...
1.找儿子
2.找父亲
只要有这两个ID, 理论上就可以将所有的article的关系遍历出来. 现实中的关系也是, 长辈/后辈, 构成了全部关系网.
显示链接内容.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% String StrId=request.getParameter("id"); int id=Integer.parseInt(StrId); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:orcl","scott","tiger"); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from article where id="+id); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% if(rs.next()) { %> <h1><font color="green"><%=rs.getString("title") %></font></h1> <hr> <h4><%=rs.getString("cont") %></h4> <% } %> </body> </html>