<%@ page language="java" import="java.util.*,java.sql.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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>
<%
String username = (String) session.getAttribute("UserName");
String Head = (String) session.getAttribute("image");
int PageSize = 3;
int PageNow = 1;//默认显示第一页
int rowCount = 0;//该值从数据库查询
int PageCount = 0;//该值是通过pageSize和rowCount
String page1 = request.getParameter("PageNow");
if (page1 != null) {
PageNow = Integer.parseInt(page1);
}
//查询得到rowCount
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接URL
String url = "jdbc:mysql://localhost:3306/XHB?useUnicode=true&characterEncoding=utf8";
Connection conn = DriverManager.getConnection(url, "root",
"5264202");
String sql1 = "select count(*) from SEvaluate";
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery(sql1);
if (rs.next()) {
rowCount = rs.getInt(1);
}
//计算pageCount,算法很多,可以自己设计
if (rowCount % PageSize == 0) {
PageCount = rowCount / PageSize;
} else {
PageCount = rowCount / PageSize + 1;
}
//查询需要的显示的记录
rs = stmt.executeQuery("select * from SEvaluate limit " + PageSize
* (PageNow - 1) + "," + PageSize + ";");
//显示
%>
<%
while (rs.next()) {
%>
<div class="liuyanlist">
<dl>
<dt>
<a href="#" class="dtrentu"><img
src="<%=rs.getString("Head")%>" border="0"> </a><a href=""
class="dthm"><%=rs.getString("UserName")%></a>
</dt>
楼层<%=rs.getInt("EvaluateId")%>
<dd>
<div class="liuyanddleft">
<p>
<a href=""><%=rs.getString("MessageBoardTitle")%> </a> <br>
<b><%=rs.getString("MessageBoardContent")%></b><br> <br>
<%=rs.getString("response")%>
</p>
</div>
</dd>
</dl>
<form name="response" action="response" method="post">
<input type="hidden" id="hid" value="<%=rs.getInt("EvaluateId")%>" name="hid"/>
<input type="button"
style="background:url(images/comment.gif)no-repeat;width:60px; height:20px;border:1px solid #fff;"
οnclick="xy(<%=rs.getInt("EvaluateId")%>);" />
<div class="from_row" id="<%=rs.getInt("EvaluateId")%>"
style="display:none;">
<div>
<textarea name="response" class="contact_input" cols="50"
rows="5"></textarea>
</div>
<input type="submit" value="提交" οnclick="xy();">
</div>
</form>
</div>
<%
}
%>
<label>
<center>
<%
//上一页
if (PageNow != 1) {
out.println("<a href=liuyanban.jsp?PageNow=" + (PageNow - 1)
+ ">上一页</a>");
}
//显示超链接
for (int i = 1; i <= PageCount; i++) {
out.println("<a href=liuyanban.jsp?PageNow=" + i + ">[" + i
+ "]</a>");
}
//下一页
if (PageNow != PageCount) {
out.println("<a href=liuyanban.jsp?PageNow=" + (PageNow + 1)
+ ">下一页</a>");
}
%>
</center> </label>
</body>
</html>
代码有点乱,但是总体思想是这个。必须给表定义一个自动增长的ID方便分页使用。
jsp用到mysql进行分页
最新推荐文章于 2019-03-17 11:46:38 发布