下载地址http://download.csdn.net/detail/hsttmht/3987324
articletree.jsp
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ page import="java.sql.*" %> <% String admin = (String)session.getAttribute("admin"); if(admin != null && admin.equals("true")) { login = true; } %> <%! String str = ""; boolean login = false; private void tree(Connection conn, int id, int level) { Statement stmt = null; ResultSet rs = null; String preStr = ""; for(int i=0; i<level; i++) { preStr += "----"; } try { stmt = conn.createStatement(); String sql = "select * from article where pid = " + id; rs = stmt.executeQuery(sql); String strLogin = ""; while(rs.next()) { if(login) { strLogin = "<td><a href='Delete.jsp?id=" + rs.getInt("id") + "&pid=" + rs.getInt("pid") + "'>删除</a>"; } str += "<tr><td>" + rs.getInt("id") + "</td><td>" + preStr + "<a href='ShowArticleDetails.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") + "</a></td>" + strLogin + "</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(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } %> <% Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/bbs"; Connection conn = DriverManager.getConnection(url,"root","root"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from article where pid = 0"); String strLogin = ""; while(rs.next()) { if(login) { strLogin = "<td><a href='Delete.jsp?id=" + rs.getInt("id") + "&pid=" + rs.getInt("pid") + "'>删除</a>"; } str += "<tr><td>" + rs.getInt("id") + "</td><td>" + "<a href='ShowArticleDetails.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") + "</a></td>" + strLogin + "</td></tr>"; if(rs.getInt("isleaf") != 0) { tree(conn, rs.getInt("id"), 1); } } rs.close(); stmt.close(); conn.close(); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>Insert title here</title> </head> <body> <table border="1"> <%= str %> <% str = ""; login = false; %> </table><br> <a href="Post.jsp">发表新帖</a> </body> </html>ShowArtciletree.jsp <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ page import="java.sql.*"%> <%!String str = ""; String strLogin = ""; boolean login = false;%> <% String admin = (String) session.getAttribute("admin"); if (admin != null && admin.equals("true")) { login = true; } %> <% int pageSize = 3; String strPageNo = request.getParameter("pageNo"); int pageNo; if (strPageNo == null || strPageNo.equals("")) { pageNo = 1; } else { try { pageNo = Integer.parseInt(strPageNo.trim()); } catch (NumberFormatException e) { pageNo = 1; } if (pageNo <= 0) pageNo = 1; } Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/bbs?user=root&password=root"; Connection conn = DriverManager.getConnection(url); Statement stmtCount = conn.createStatement(); ResultSet rsCount = stmtCount .executeQuery("select count(*) from article where pid = 0"); rsCount.next(); int totalRecords = rsCount.getInt(1); int totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1; if (pageNo > totalPages) pageNo = totalPages; int startPos = (pageNo - 1) * pageSize; Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("select * from article where pid = 0 order by pdate desc limit " + startPos + "," + pageSize); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>Insert title here</title> </head> <body> <a href="Post.jsp">发表新帖</a> <br> <table border="1"> <% while (rs.next()) { if (login) { strLogin = "<td><a href='Delete.jsp?id=" + rs.getInt("id") + "&pid=" + rs.getInt("pid") + "'>删除</a>"; } str += "<tr><td>" + "<a href='ShowArticleDetails.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") + "</td><td>" + strLogin + "</td></tr>"; ; } rs.close(); stmt.close(); conn.close(); %> <%=str%> <% str = ""; %> </table> <br> 共<%=totalPages%>页 第<%=pageNo%>页 <br> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo - 1%>"> < </a> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo + 1%>"> > </a> <form name="form1" action="ShowArticleFlat.jsp"> <select name="pageNo" οnchange="document.form1.submit()"> <% for (int i = 1; i <= totalPages; i++) { %> <option value=<%=i%> <%=(pageNo == i) ? "selected" : ""%>> 第<%=i%>页 <% } %> </select> </form> <form name="fom2" action="ShowArticleFlat.jsp"> <input type=text size=4 name="pageNo" value=<%=pageNo%> /> <input type="submit" value="go" /> </form> </body> </html>
showarticledetails.jsp <%@ page language="java" import="java.sql.*" pageEncoding="GBK"%> <html> <head> <title>JDBC</title> </head> <body> <%!ResultSet rs = null; PreparedStatement pstmt = null; Connection conn = null;%> <% String strid = request.getParameter("id"); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bbs", "root", "root"); Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("select * from article where pid = " + strid); while (rs.next()) { %> <table width="500" align="center" border="2" bgcolor="#FFFF00"> <tr> <td> ID </td> <td><%=rs.getInt("id")%></td> </tr> <tr> <td> Title </td> <td><%=rs.getString("title")%></td> </tr> <tr> <td> Content </td> <td><%=rs.getString("cont")%></td> </tr> </table> <a href="Reply.jsp?id=<%=rs.getInt("id")%>&rootid=<%=rs.getInt("rootid")%>">replay</a> <% } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> </body> </html>delete.jsp <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.sql.*" %> <%! private void del(Connection conn, int id) { Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); String sql = "select * from article where pid = " + id; rs = stmt.executeQuery(sql); while(rs.next()) { del(conn , rs.getInt("id")); } stmt.executeUpdate("delete from article where id = " + id); } 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(); } } } %> <% String admin = (String)session.getAttribute("admin"); if(admin == null || !admin.equals("true")) { out.println("对不起,你无权删除本帖!"); return; } %> <% 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"; Connection conn = DriverManager.getConnection(url,"root","1234"); conn.setAutoCommit(false); del(conn , id); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select count(*) from article where pid=" + pid); rs.next(); int count = rs.getInt(1); rs.close(); stmt.close(); if(count <= 0) { Statement stmtUpdate = conn.createStatement(); stmtUpdate.executeUpdate("update article set isleaf = 0 where id = " + pid); stmtUpdate.close(); } conn.commit(); conn.setAutoCommit(true); conn.close(); response.sendRedirect("articletree.jsp"); %>
reply.jsp <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.sql.*"%> <% int id = Integer.parseInt(request.getParameter("id")); int rootid = Integer.parseInt(request.getParameter("rootid")); %> <body> <form action="ReplyOK.jsp" method="post"> <input type="hidden" name="id" value="<%=id%>" /> <input type="hidden" name="rootid" value="<%=rootid%>" /> <table> <tr> <td> 标题: <input type="text" name="title"> </td> </tr> <tr> <td> 内容: <textarea name="cont" rows="15" cols="80"></textarea> </td> </tr> <tr> <td> 提交: <input type="submit" value="submit" /> </td> </tr> </table> </form> </body>
replyOK.jsp <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.sql.*"%> <% request.setCharacterEncoding("GBK"); int id = Integer.parseInt(request.getParameter("id")); int rootid = Integer.parseInt(request.getParameter("rootid")); String title = request.getParameter("title"); String cont = request.getParameter("cont"); cont = cont.replaceAll("\n", "<br>"); Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bbs", "root", "root"); conn.setAutoCommit(false); String sql = "insert into article values (null, ?, ?, ?, ?, now(), 0)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setInt(2, rootid); pstmt.setString(3, title); pstmt.setString(4, cont); pstmt.executeUpdate(); Statement stmt = conn.createStatement(); stmt.executeUpdate("update article set isleaf = 1 where id = " + id); conn.commit(); conn.setAutoCommit(true);//事务处理 stmt.close(); pstmt.close(); conn.close(); %> 请点击下面链接,跳转到<a href="articletree.jsp">主题列表</a>
login.jsp <%@ page contentType="text/html; charset=gbk" pageEncoding="gbk" %> <% String action = request.getParameter("action"); if(action != null && action.equals("login")) { String username = request.getParameter("username"); String password = request.getParameter("password"); if(username == null || !username.equals("admin")) { out.println("username not correct!"); }else if(password == null || !password.equals("admin")) { out.println("password not correct!"); }else { session.setAttribute("admin", "true"); response.sendRedirect("articletree.jsp"); } } %> <form action="Login.jsp" method="post"> <input type="hidden" name="action" value="login"> <table border="1px"> <tr><td>UserName:</td><td><input type="text" name="username"></td> <tr><td>PassWord:</td><td><input type="password" name="password"></td> <tr><td></td><td><input type="submit" value="submit"></td></tr> </table> </form>
post.jsp <%@ page pageEncoding="GBK"%> <%@ page import="java.sql.*"%> <% request.setCharacterEncoding("GBK"); String action = request.getParameter("action"); if (action != null && action.trim().equals("post")) { String title = request.getParameter("title"); String cont = request.getParameter("cont"); cont = cont.replaceAll("\n", "<br>"); Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bbs", "root", "root"); conn.setAutoCommit(false); int rootId = -1; String sql = "insert into article values (null, ?, ?, ?, ?, now(), ?)"; PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstmt.setInt(1, 0); pstmt.setInt(2, rootId); pstmt.setString(3, title); pstmt.setString(4, cont); pstmt.setInt(5, 0); pstmt.executeUpdate(); ResultSet rsKey = pstmt.getGeneratedKeys(); rsKey.next(); rootId = rsKey.getInt(1); Statement stmt = conn.createStatement(); stmt.executeUpdate("update article set rootid = " + rootId + " where id = " + rootId); conn.commit(); conn.setAutoCommit(true); pstmt.close(); stmt.close(); conn.close(); response.sendRedirect("ShowArticleDetails.jsp"); } %> <body> 发表新贴 <form action="post.jsp" method="post"> <input type="hidden" name="action" value="post" /> <table> <tr> <td> 标题: <input type="text" name="title"> </td> </tr> <tr> <td> 内容: <textarea name="cont" rows="15" cols="80"></textarea> </td> </tr> <tr> <td> 提交: <input type="submit" value="submit" /> </td> </tr> </table> </form> </body>