<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*,message.Message"%>
<HTML>
<head>
<title>留言板</title>
<style type="text/css">
input[type="submit"] {
width: 20%;
color: white;
border: none;
cursor: pointer;
font-size: 24px;
background-color: #4cae4c;
}
</style>
</head>
<BODY>
<div style=" background-color:#66ccff;position: absolute;top:10px;right:100px;">
<a style="text-decoration: none;"href="AAAShouYe.jsp">返回首页</a>
</div>
<%
Message M = new Message();
int pageSize = 10;
int currentPage = Integer.parseInt(request.getParameter("page") != null ? request.getParameter("page") : "1");
int totalRecords = 0; // 用于存储总记录数
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String driverName = "com.mysql.jdbc.Driver";
String uri = "jdbc:mysql://localhost:3306/mis?serverTimezone=UTC";
String user = "root";
String password = "123456";
try {
Class.forName(driverName);
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
int offset = (currentPage - 1) * pageSize;
rs = stmt.executeQuery("SELECT * FROM message ORDER BY datetime DESC LIMIT " + pageSize + " OFFSET " + offset);
totalRecords=M.totalRecords();
int totalPages = (totalRecords + pageSize - 1) / pageSize;
%>
<h1 style="text-align:center;">留言板</h1>
<br>
留言板上共有
<FONT SIZE=4 COLOR=red><%= totalRecords %></FONT>
条留言
<br>
<table width="90%" border="0" cellpadding="2" cellspacing="1" style="word-break: break-all;"align=center>
<TR bgcolor=CCCCCC ALIGN=CENTER>
<TD width="5%">
<B>记录条数</B>
</TD>
<TD width="20%">
<B>发布时间</B>
</TD>
<TD width="10%">
<B>姓名</B>
</TD>
<TD width="55%">
<B>留言内容</B>
</TD>
</TR>
<%
rs.beforeFirst();
while (rs.next()) {
%>
<TR ALIGN=CENTER>
<TD width="5%"><br>
<B><%=rs.getRow()%></B>
</TD>
<TD width="20%"><br>
<B><%=rs.getTimestamp("datetime")%></B>
</TD>
<TD width="10%"><br>
<B><%=rs.getString("name")%></B>
</TD>
<TD width="55%"><br>
<B><%=rs.getString("message")%></B>
</TD>
</TR>
<%
}
rs.close();
stmt.close();
con.close();
%></table>
<div align="center">
<a href="?page=1">第一页</a>
<% for (int p = 1; p <= totalPages; p++) { %>
<a href="?page=<%= p %>"><%= p %></a>
<% if (p < totalPages) out.print(" "); %>
<% } %>
<a href="?page=<%= totalPages %>">尾页</a>
</div>
<%
} catch (ClassNotFoundException e) {
out.print("连接失败!");
e.printStackTrace();
} catch (SQLException e) {
out.print("数据库查询错误!");
e.printStackTrace();
} finally {
// 关闭资源
try { if (rs != null) rs.close(); } catch (SQLException e) {}
try { if (stmt != null) stmt.close(); } catch (SQLException e) {}
try { if (con != null) con.close(); } catch (SQLException e) {}
}
%> <br><br>
<form action="writeMessage.jsp" method="post" accept-charset="UTF-8">
<input type="submit" value="写留言">
</form>
</BODY>
</HTML>