关于BBS项目相关代码

ShowArticleTree.jsp

<%@page import="com.sun.org.apache.regexp.internal.recompile"%>
<%@page import="com.sun.jndi.cosnaming.CNNameParser"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ page import="java.sql.*"%>
<%!String str = "";

	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;
			System.out.println(sql);
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				str += "<tr><td>" + rs.getInt("id") + "</td><td>" + preStr
						+ "<a href='ShowArticleDetail.jsp?id="
						+ rs.getInt("id") + "'>" + rs.getString("title")
						+ "</a></td>" + "<td><a href ='Delete.jsp?id="
						+ rs.getInt("id") + "&pid=" + rs.getInt("pid")
						+ "'>删除</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 (stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}%>
<%
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
	Connection conn = DriverManager.getConnection(url);
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt
			.executeQuery("select *  from article  where  pid=0");
	while (rs.next()) {
		str += "<tr><td>" + rs.getInt("id") + "</td><td>"
				+ "<a href='ShowArticleDetail.jsp?id="
				+ rs.getInt("id") + "'>" + rs.getString("title")
				+ "</a></td>" + "<td><a href ='Delete.jsp?id="
				+ rs.getInt("id") + "&pid=" + rs.getInt("pid")
				+ "'>删除</a>" + "</td></tr>";
		if (rs.getInt("isleaf") != 0) {
			tree(conn, rs.getInt("id"), 1);
		}

	}
	rs.close();
	stmt.close();
	conn.close();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'ShowArticleTree.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>
	<table border="1">
		<%=str%>
		<%
			str = "";
		%>
	</table>

</body>

</html>

ShowArticleDetail.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.* " %>
<%
     String strId = request.getParameter("id");
     int id = Integer.parseInt(strId);
   
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
	Connection conn = DriverManager.getConnection(url);
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt
			.executeQuery("select *  from article  where  id=" + id);


%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ShowArticleDetail.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>
<%
     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>Content  </td>
                <td> <%= rs.getString("cont") %></td>
         </tr>
         </table>
         <a href="Replay.jsp?id=<%= rs.getInt("id")%>&rootid=<%= rs.getInt("rootid")%>">回复</a>
 <% 
     }
     rs.close();
	stmt.close();
	conn.close();
 %>
  </body>
</html>

Replay.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'Replay.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>
<form action="ReplayOK.jsp" method="post">
         <br><input type="hidden"  name="id"  value="<%=id %>">
         <input type="hidden"  name="rootid"  value="<%= rootId %>">
        <table border="1">
                     <tr>
                             <td>
                                  <input type="text"  name="title"  size="80">
                            </td>
                           </tr>  
                           <tr>
                             <td>
                                      <textarea cols="80"  rows="12"  name="cont"></textarea>
                           </td>
                           </tr>
                           <tr> 
                             <td>
                                      <input type="submit"  value="提交">
                           </td>
                    </tr>
         </table>
</form>
</body>
</html>



ReplayOK.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"  %>
<%
    request.setCharacterEncoding("UTF-8");//改用统一的编码,此前数据库内是乱码,得知插入时出错
    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");
	String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
	Connection conn = DriverManager.getConnection(url);//连接数据库
	
	conn.setAutoCommit(false);//禁止自动提交
	
	String sql= "insert into article values ( null , ?, ? , ? , ? , now() , 0)";
	PreparedStatement pstmt= conn.prepareStatement(sql);
	Statement stmt= conn.createStatement();
	
	pstmt.setInt(1,id);
	pstmt.setInt(2, rootId);
	pstmt.setString(3, title);
	pstmt.setString(4, cont);
	pstmt.executeUpdate();//执行以上sql语句
	
    stmt.executeUpdate("update article set isleaf  =1 where id="+id);
    
    
	conn.commit(); 
	conn.setAutoCommit(true);//全部提交后设置自动提交
	
	stmt.close(); 
	pstmt.close();
	conn.close();
	
	response.sendRedirect("ShowArticleTree.jsp");
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ReplayOK.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>
    <font color ="red" size="5"></font>
    回复成功!
    
  </body>
</html>

Delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ 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;
			System.out.println(sql);
			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();
			}
		}

	}%>
<%
	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/bbs?user=root&password=root";
	Connection conn = DriverManager.getConnection(url);//连接数据库

	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("ShowArticleTree.jsp");
%>



FlatPost.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ page import="java.sql.*"%>
<%
    request.setCharacterEncoding("UTF-8");//改用统一的编码,此前数据库内是乱码,得知插入时出错
    String action = request.getParameter("action");
    if (action != null && action.equals("post")) {
        String title = request.getParameter("title");
        String cont = request.getParameter("cont");

        cont = cont.replaceAll("\n", "<br>");//替换/n 为换行

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
        Connection conn = DriverManager.getConnection(url);//连接数据库

        conn.setAutoCommit(false);//禁止自动提交

        String sql = "insert into article values ( null , 0, ? , ? , ? , now() , 0)";
        PreparedStatement pstmt = conn.prepareStatement(sql,
                Statement.RETURN_GENERATED_KEYS);
        Statement stmt = conn.createStatement();

        pstmt.setInt(1, -1);
        pstmt.setString(2, title);
        pstmt.setString(3, cont);
        pstmt.executeUpdate();//执行以上sql语句

        ResultSet rsKey = pstmt.getGeneratedKeys();
        rsKey.next();
        int key = rsKey.getInt(1);
        rsKey.close();
        stmt.executeUpdate("update article set rootid = " + key + " where id = " + key);

        conn.commit();
        conn.setAutoCommit(true);//全部提交后设置自动提交

        stmt.close();
        pstmt.close();
        conn.close();

        response.sendRedirect("ShowArticleFlat.jsp");
    }
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Replay.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>
    <form action="Post.jsp" method="post">
        <input type="hidden" name="action" value="post">

        <table border="1">
            <tr>
                <td><input type="text" name="title" size="80">
                </td>
            </tr>
            <tr>
                <td><textarea cols="80" rows="12" name="cont"></textarea>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>



ShowArticleFlat.jsp



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ page import="java.sql.*"%>
<%
    request.setCharacterEncoding("UTF-8");//改用统一的编码,此前数据库内是乱码,得知插入时出错
    String action = request.getParameter("action");
    if (action != null && action.equals("post")) {
        String title = request.getParameter("title");
        String cont = request.getParameter("cont");

        cont = cont.replaceAll("\n", "<br>");//替换/n 为换行

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
        Connection conn = DriverManager.getConnection(url);//连接数据库

        conn.setAutoCommit(false);//禁止自动提交

        String sql = "insert into article values ( null , 0, ? , ? , ? , now() , 0)";
        PreparedStatement pstmt = conn.prepareStatement(sql,
                Statement.RETURN_GENERATED_KEYS);
        Statement stmt = conn.createStatement();

        pstmt.setInt(1, -1);
        pstmt.setString(2, title);
        pstmt.setString(3, cont);
        pstmt.executeUpdate();//执行以上sql语句

        ResultSet rsKey = pstmt.getGeneratedKeys();
        rsKey.next();
        int key = rsKey.getInt(1);
        rsKey.close();
        stmt.executeUpdate("update article set rootid = " + key + " where id = " + key);

        conn.commit();
        conn.setAutoCommit(true);//全部提交后设置自动提交

        stmt.close();
        pstmt.close();
        conn.close();

        response.sendRedirect("ShowArticleFlat.jsp");
    }
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Replay.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>
    <form action="Post.jsp" method="post">
        <input type="hidden" name="action" value="post">

        <table border="1">
            <tr>
                <td><input type="text" name="title" size="80">
                </td>
            </tr>
            <tr>
                <td><textarea cols="80" rows="12" name="cont"></textarea>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>





  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值