一個簡單的練習,沒有做什麼前台,用到了遞迴,參數傳遞,資料庫操作,session以及字元編碼轉換。
其中有兩個主要問題費了一些時間:
1.ResultSet對象的getRow()方法是返回指標指向的行,next()可以移動到下一行
2.字元轉換的問題可以用 new String(Str.getBytes("ISO-8859-1")) ,有時候request.setCharacterEncoding("GB2312")轉換的時候不是很有效。還是太菜,有待考證。
--bbs.sql --建立資料庫 create database bbs; use bbs; --建立表 --使用 create table article( id int primary key auto_increment, --父節點id pid int, --根結點id rootid int, title varchar(255), cont text, pdate datetime, --是否是葉子節點 isleaf int ); --0 代表leaf ,1代表非leaf insert into article values (null,0,1,'螞蟻大戰大象','螞蟻大戰大象',now(),1); insert into article values (null,1,1,'大象被打趴下了','大象被打趴下了',now(),1); insert into article values (null,2,1,'螞蟻也不好過','螞蟻也不好過',now(),0); insert into article values (null,2,1,'瞎說','瞎說',now(),1); insert into article values (null,4,1,'沒有瞎說','沒有瞎說',now(),0); insert into article values (null,1,1,'怎麼可能','怎麼可能',now(),1); insert into article values (null,6,1,'怎麼沒有可能','怎麼沒有可能',now(),0); insert into article values (null,6,1,'可能性是很大的','可能性是很大的',now(),0); insert into article values (null,2,1,'大象進醫院了','大象進醫院了',now(),1); insert into article values (null,9,1,'護士是螞蟻','護士是螞蟻',now(),0);
//ShowArticleTree.jsp <%@ page language="java" contentType="text/html;charset=gb2312" pageEncoding="gb2312"%> <%@ page import="java.sql.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root"; Connection conn = DriverManager.getConnection(url); Statement smt = conn.createStatement(); ResultSet rs = smt.executeQuery("select * from article where pid=0"); while( rs.next() ){ str = "<tr><td>" + rs.getInt("id") + "</td><td>" + "<a href='ArticleDetail.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") + "</a>" + "</td><td>" + "<a href='Delete.jsp?id=" + rs.getInt("id") + "'>" + "刪除</a>"+ "</td></tr>"; if( rs.getInt("isleaf") != 0){ Tree(conn,rs.getInt("id"),1); } } rs.close(); smt.close(); conn.close(); %> <%! String str=""; private void Tree(Connection conn,int id,int level) { String preString = ""; Statement stm = null; ResultSet rs = null; for(int i=0;i<level;i++){ preString += "----"; } try{ stm = conn.createStatement(); rs = stm.executeQuery("select * from article where pid="+ id ); while(rs.next()){ str += "<tr><td>" + rs.getInt("id") + "</td><td>" + preString + "<a href='ArticleDetail.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") + "</a></td>"+ "<td>" + "<a href='Delete.jsp?id=" + rs.getInt("id") + "'>" + "刪除</a>"+ "</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(stm != null){ stm.close(); stm = null; } }catch(SQLException e){ e.printStackTrace(); } } } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>論壇的樹狀結構</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my BBS page. <br> <table border="1"> <%= str %> </table> </body> </html>
//ArticleDetail.jsp <%@ page language="java" contentType="text/html;charset=gbk" pageEncoding="gbk"%> <%@ page import=" java.sql.* " %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String paraId=request.getParameter("id"); int id = Integer.parseInt(paraId); Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root"; Connection conn = DriverManager.getConnection(url); Statement smt = conn.createStatement(); ResultSet rs = smt.executeQuery("select * from article where id=" + id); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>主題的標題和內容</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% if( rs.next() ) { %> <table border=1> <tr> <td>ID</td> <td><%= rs.getInt("id") %></td> </tr> <tr> <td>Title</td> <td><%= rs.getString("title") %></td> </tr> <tr> <td>Cont</td> <td><%=rs.getString("Cont")%></td> </tr> </table> <a href="Reply.jsp?id=<%=rs.getInt("id") %>&rootid=<%=rs.getInt("rootid") %>">回複</a> <a href="Delete.jsp?id=<%=rs.getInt("id") %>">刪除</a> <% } %> <% rs.close(); smt.close(); rs.close(); %> </body> </html>
//Delete.jsp <%@ page language="java" contentType="text/html;charset=GB2312" pageEncoding="GB2312"%>
<%@ page import="java.sql.*" %> <%!
private void delete(Connection conn,int id) {
Statement stm = null;
ResultSet rs = null;
try{
stm = conn.createStatement();
String sql = "select * from article where pid=" + id;
rs = stm.executeQuery(sql);
while( rs.next() ) {
delete(conn , rs.getInt("id") );
}
stm.executeUpdate("delete from article where id=" + id);
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
if( rs != null) {
rs.close();
rs = null;
}
if( stm != null ) {
stm.close();
stm = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
} %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid")); Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false); delete(conn,id); conn.commit();
conn.setAutoCommit(true); String sql = "select count(*) from article where pid=" + pid;
Statement stm1 = conn.createStatement();
ResultSet rs = stm1.executeQuery(sql);
rs.next();
int count = rs.getInt(1);
if(count <=0 ) {
stm1.executeUpdate("update article set isleaf=0 where id=" + pid);
}
rs.close();
stm1.close();
conn.close();
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Delete.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<% response.sendRedirect("ShowArticleTree.jsp"); %>
</body>
</html>
//Reply.jsp <%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; int id = Integer.parseInt(request.getParameter("id")); int rootid = Integer.parseInt(request.getParameter("rootid")); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>回複頁面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv=