jsp分页的时候只显示前五条,第六条没有显示,这是什么问题?
波西塔塔Y 2015-04-23 12:25:08
try {
String page1 = request.getParameter("page");//获得页面传递过来的page值赋值给page1
int page2 =1;
if(page1 != null){
//如果页面传递过来的page值存在,则把该page1赋值给page2变量
page2 = Integer.parseInt(page1);
}
request.setAttribute("page2", page2);
//分页查询的SQL语句,每页显示5条记录
String sql = "select * from _teacher order by id desc limit?,5";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, page2);
rs = pstmt.executeQuery();
while(rs.next()){
TeacherVO teacherVo = new TeacherVO();//声明Vo对象,把数据放入该对象中
list.add(teacherVo);//把Vo对象添加到List集合中
request.setAttribute("list", list);
}
int count =0;//声明一个count变量,用于存储记录数
int maxpage = 0;//声明一个maxpage变量,原来表示最大页数
Statement stmt = conn.createStatement();
String sql1 ="select count(*) from _teacher";//查询总记录数的SQL语句
rs = stmt.executeQuery(sql1);
while (rs.next()) {
count = rs.getInt(1);//如果结果集存在,记录数初始化值为1
}
maxpage = (count+4)/5;//最大页数等于总记录条数加上4,再除以5
request.setAttribute("maxpage", maxpage);
} catch (SQLException e) {
e.printStackTrace();
}
%>
<body>
<table border="1" align="center" width="70%">
<tr>
<td>教师工号</td>
<td>联系方式</td>
</tr>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.id }</td>
<td>${list.tnum }</td>
</tr>
</c:forEach>
</table>
<div align="center">
<a href="?page=1">首页</a>
<c:if test="${page2 ==1}">上一页</c:if>
<c:if test="${page2 > 1}"><a href="?page=${page2 -1 }">上一页</a></c:if>
<c:if test="${page2 == maxpage}">下一页</c:if>
<c:if test="${page2 < maxpage}"><a href="?page=${page2 +1 }">下一页</a></c:if>
<a href="?page=${maxpage }">末页</a>