BBS
tiezi。jsp
<%@ page pageEncoding="GB18030"%>
<%@ page
import=" com.jingtian.db.*,com.jingtian.javabean.*,java.util.*,java.sql.*"%>
<!--URL传值用?或者form表格形式.用request.getParameter("id")获取,但都是字符串形式 -->
<!-- 时间转化 -->
<%--=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate())--%>
<%
request.setCharacterEncoding("GBK");
int i = 1;
List<Article> list = new ArrayList<Article>();
String str = "select * from article where pid=0";
ResultSet rs = DB.executeQuery(str);
while (rs.next())//游标由0指向第一条数据
{
Article article = new Article();
article.init(rs);//一一对应
list.add(article);
}
%>
<html>
<head>
<title>My JSP 'tiezi.jsp' starting page</title>
</head>
<body>
<table border="2" width="70%">
<caption>主题</caption>
<a href="newzhuti.jsp">发表新主题</a>
<%
for (Article a : list) {
%>
<tr>
<td><a href="zhankai.jsp?id=<%=a.getId()%>"> <%="【" + i++ + "】" + a.getCont()%> </a></td>
<td><%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate())%></td>
<td><a href="delete.jsp?rootid=<%=a.getRootid()%>">删除</a>
</td>
</tr>
<%
}
%>
</table>
<a href="tieziFlat.jsp">分页显示</a>
</body>
</html>
zhankai。jsp
<%@ page pageEncoding="GB18030"%>
<%@ page import="com.jingtian.db.*,com.jingtian.javabean.*,java.util.*,java.sql.*"%>
<!-- sql语句就是字符串需要写单引号 -->
<%
int i = 0;
int id = Integer.parseInt(request.getParameter("id"));//String强转化为int类型
List<Article> list = new ArrayList<Article>();
treeBianLi(list, id,0);
%>
<%
Article articleFu = null;
String str = "select * from article where id=" + id;
ResultSet rs = DB.executeQuery(str);
while (rs.next())//光标由0指向第一条数据
{
articleFu = new Article();
articleFu.init(rs);//一一对应
}
%>
<%!private void treeBianLi(List<Article> list, int id,int grade) {
String str = "select * from article where pid=" + id + "";
ResultSet rs = DB.executeQuery(str);
try {
while (rs.next()) {
Article article = new Article();//覆盖了,为什么
article.init(rs);
article.setGrade(grade);
list.add(article);
// System.out.print(article.getCont());
if (!article.getIsLeaf()) {
treeBianLi(list, article.getId(),grade+1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
<html>
<head>
<title>My JSP 'tiezi.jsp' starting page</title>
</head>
<body>
<table border="2" width="70%">
<tr>
<td>主题</td>
</tr>
<tr>
<td>
<%
i++;
out.print("【" + i + "楼】");
%>
</td>
<td><%=articleFu.getCont()%></td><td><%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(articleFu.getPdate())%></td>
<td><a href="reply.jsp?id=<%=articleFu.getId()%>&rootid=<%=articleFu.getRootid()%>">回复</a></td>
</tr>
<%
for (Article a : list) {System.out.print(a.getGrade());
String pstr="---------";
for(int j=0;j<a.getGrade();j++)
{
pstr=pstr+"---------";
}
%>
<tr>
<td>
<%
i++;
out.print("【" + i + "楼】");
a.setGrade(i);
%>
</td>
<td>
<%
out.print(pstr+""+a.getCont());
%>
</td><td><%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate())%></td>
<td><a
href="reply.jsp?id=<%=a.getId()%>&rootid=<%=a.getRootid()%>">回复</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
reply。jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
int rootid = Integer.parseInt(request.getParameter("rootid"));
%>
<html>
<head>
<title>My JSP 'reply.jsp' starting page</title>
</head>
<body>
<form action="replyDatabase.jsp" method="post">
<input type="hidden" name="pid" value="<%=id%>"/>
<input type="hidden" name="rootid" value="<%=rootid%>"/>
标题:<input type="text" name="title"><br>
内容:<textarea name="cont" rows="15" cols="80"></textarea><br>
<input type="submit" value="submit">
</form>
</body>
</html>
replyDatabase。jsp
<%@ page pageEncoding="GB18030"%>
<%@ page
import=" com.jingtian.db.*,com.jingtian.javabean.*,java.sql.*"%>
<!-- 回复之后,插入的节点的父亲结点就不是叶子了 -->
<!-- 回复要有pid和rootid,pid用来初始化新的节点,rootid用来遍历-->
<!-- sql插入当前时间用now() -->
<%
request.setCharacterEncoding("GBK");
int pid = Integer.parseInt(request.getParameter("pid"));
int rootid = Integer.parseInt(request.getParameter("rootid"));
String title = request.getParameter("title");
String cont = request.getParameter("cont");
String str1="update article set isLeaf =1 where id="+pid;
DB.executeUpdate(str1);
String str2="insert into article values(null,'"+pid+"','"+rootid+"','"+title+"','"+cont+"',now(),'0')";
DB.executeUpdate(str2);
response.sendRedirect("zhankai.jsp?id="+rootid);
%>
<html>
<head>
<title>My JSP 'tiezi.jsp' starting page</title>
</head>
<body>
</body>
</html>
newzhuti。jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import=" com.jingtian.db.*,com.jingtian.javabean.*,java.sql.*"%>
<!-- 新主题pid=0;id和rootid相等 -->
<%
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
if (action != null &&action.equals("post")) {
String title = request.getParameter("title");
String cont = request.getParameter("cont");
String str1="insert into article values(null,'0','1','"+title+"','"+cont+"',now(),'0')";
DB.executeUpdate(str1);
String str2="select * from article where pid=0";
ResultSet rs=DB.executeQuery(str2);
while(rs.next())
{
int id=rs.getInt("id");
String str3="update article set rootid ="+id+" where id="+id;
DB.executeUpdate(str3);
}
response.sendRedirect("tiezi.jsp");
}
%>
<html>
<head>
<title>My JSP 'reply.jsp' starting page</title>
</head>
<body>
<form action="newzhuti.jsp" method="post">
<input type="hidden" name="action" value="post" />
标题:<input type="text" name="title"><br>
内容:<textarea name="cont" rows="15" cols="80"></textarea>
<br> <input type="submit" value="submit">
</form>
</body>
</html>
delete。jsp
<%@page import=" com.jingtian.db.DB"%>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
int rootid = Integer.parseInt(request.getParameter("rootid"));
String str = "delete from article where rootid=" + rootid;//"delete from article where id = " + id
DB.executeUpdate(str);
response.sendRedirect("tiezi.jsp");
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
</body>
</html>
tieziFlat。jsp
<%@ page pageEncoding="GB18030"%>
<%@ page
import=" com.jingtian.db.*,com.jingtian.javabean.*,java.util.*,java.sql.*"%>
<%
int i = 1;
final int PAGE_SIZE = 4;
int pageNo = 1;
String strPageNo = request.getParameter("pageNo");
if (strPageNo != null && !strPageNo.trim().equals("")) {
try {
pageNo = Integer.parseInt(strPageNo);
} catch (NumberFormatException e) {
pageNo = 1;
}
}
if(pageNo <= 0) pageNo = 1;
int totalPages = 0;
ResultSet rsCount = DB.executeQuery("select count(*) from article where pid = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);
/* ResultSet rsCount2 = DB.executeQuery("select * from article where pid = 0");
int totalRecords2=0;
while(rsCount2.next())
{
totalRecords2++;
}
System.out.print(totalRecords2); */
totalPages = (totalRecords + PAGE_SIZE-1)/PAGE_SIZE;
/* if(totalRecords % PAGE_SIZE>=1)
totalPages = totalRecords/ PAGE_SIZE+1;
else
totalPages = totalRecords/ PAGE_SIZE;*/
if (pageNo > totalPages)pageNo = totalPages;
int startPos = (pageNo - 1) * PAGE_SIZE;
String sql = "select * from article where pid = 0 order by pdate desc limit "+ startPos + "," + PAGE_SIZE;
ResultSet rs = DB.executeQuery(sql);
List<Article> list = new ArrayList<Article>();
while (rs.next()) {
Article a = new Article();
a.init(rs);
list.add(a);
}
%>
<html>
<head>
<title>My JSP 'tiezi.jsp' starting page</title>
</head>
<body>
<table border="2" width="70%">
<caption>主题</caption>
<a href="newzhuti.jsp">发表新主题</a>
<%
for (Article a : list) {
%>
<tr>
<td><a href="zhankai.jsp?id=<%=a.getId()%>"> <%="【" + i++ + "】" + a.getCont()%> </a></td>
<td><%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(a.getPdate())%></td>
<td><a href="delete.jsp?rootid=<%=a.getRootid()%>">删除</a>
</td>
</tr>
<%
}
%>
</table>
<br>
<a href="tieziFlat.jsp?pageNo=1">第一页</a>
<a href="tieziFlat.jsp?pageNo=<%=pageNo-1%>">上一页</a>
<a href="tieziFlat.jsp?pageNo=<%=pageNo+1%>">下一页</a>
<a href="tieziFlat.jsp?pageNo=<%=totalPages%>">最后一页</a>
</body>
</html>