1.导入表格模板 %@ page language= " java " contentType= " text/html; charset=utf-8 " pageEncoding = " utf-8 " %%@ page import= " java.sql.* " %% Class.forName( " com.mysql.jdbc.Driver " ); Connection conn =DriverManager.getConnection(
1.导入表格模板
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <% Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root"); PreparedStatement ps=conn.prepareStatement("select * from article"); ResultSet rs=ps.executeQuery(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ShowArticleTree</title> </head> <body> <table border="1"> <% while(rs.next()){ %> <tr> <td><%=rs.getInt("id") %></td> <td><%=rs.getString("cont") %></td> </tr> <% } %> </table> </body> <% rs.close(); ps.close(); conn.close(); %> </html>
2.树形利用递归进行遍历算法,求根帖子
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <%! String str=""; private void tree(Connection conn,int id,int level){ PreparedStatement ps=null; ResultSet rs=null; String preStr=""; for(int i=0;i<level;i++){ preStr+="****"; } try{ String sql="select * from article where pid ="+id; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ str+="<tr><td>"+rs.getInt("id")+"</td><td>"+preStr+ rs.getString("cont")+"</td></tr>"; if(rs.getInt("isleaf")!=0){ tree(conn,rs.getInt("id"),level+1); } } }catch(SQLException e){ e.printStackTrace(); }finally{ try{ if(rs!=null) { rs.close(); rs=null; } if(ps!=null) { ps.close(); ps=null; } }catch(SQLException e2){ e2.printStackTrace(); } } } %> <% Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root"); PreparedStatement ps=conn.prepareStatement("select * from article where pid=0"); ResultSet rs=ps.executeQuery(); while(rs.next()){ str+="<tr><td>"+rs.getInt("id")+"</td><td>"+rs.getString("cont")+"</td></tr>"; if(rs.getInt("isleaf")!=0){ tree(conn,rs.getInt("id"),1); } } rs.close(); ps.close(); conn.close(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ShowArticleTree</title> </head> <body> <table border="1"> <%=str%> </table> </body> </html>