我用的是 预处理语句 preparedStatement 请注意下面的 语句创建的时候 要用重载的方法 加上如下字段 可以是 结果集指定 任意移动 以用来确定显示 指定的 结果集范围
在 ptmt = con.prepareStatement(preparedSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);//允许结果集指针移动
代码参考如下:
<%@ page language ="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="bankel.lyf.*,java.sql.*" %>
<html>
<% request.setCharacterEncoding("UTF-8");
int originalPage = 1;
String serverPage = request.getParameter("disPage");
try{
originalPage = Integer.parseInt(serverPage);
}catch(NumberFormatException e){
originalPage = 1;
}
%>
<head>
<title>分页显示查询到的图书</title>
<link rel="stylesheet" type="text/css" href="template.css"/>
</head>
<body>
<%
ResultSet rs = null;
BookDao bookDao = new BookDao();
bookDao.getConnection("localhost", "orcl", "scott", "tiger");
String sql = "select * from book";
rs = bookDao.executeQuery(sql,null);
%>
<%
int allPageCount; //总页数
int everyPageRecord=5; //每页的记录
int currentPage = originalPage; //当前页页码
int allRecord; //总记录条数
rs.last();
allRecord=rs.getRow();
if(allRecord%everyPageRecord==0){
allPageCount=allRecord/everyPageRecord;
}
else{
allPageCount = allRecord/everyPageRecord + 1;
}
//移动记录指针到当前页之前
if(currentPage==1){
rs.beforeFirst();
}
else{
rs.absolute((currentPage-1)*5);
}
%>
<table id="coustom">
<tr >
<td colspan="6" style="text-align:center">
一共查询到<%=allRecord %>条记录
</td>
</tr>
<tr>
<td>序号</td><td>书名</td><td>出版社</td><td>简介</td><td>价格</td><td>数量</td>
</tr>
<%
int recordNumber=(currentPage-1)*5+1;
int first = recordNumber;
while(rs.next())
{
%>
<tr>
<td><%=recordNumber++%></td>
<td><%=rs.getString("bookname")%></td>
<td><%=rs.getInt("publisherid")%></td>
<td><%=rs.getString("description")%></td>
<td><%=rs.getDouble("price")%></td>
<td><%=rs.getInt("count")%></td>
</tr>
<%
if(recordNumber-first>=5){
break;
}
}
%>
<tr>
<td colspan="6" style="text-align:center">有<%=allRecord %>条记录 ,每页<%=everyPageRecord%>条记录, 共<%=allPageCount %>页
当前是第<%=currentPage %>页
<%
if(currentPage==1)
{
%>
<a href="paging.jsp?disPage=<%=String.valueOf(currentPage+1)%>">下一页</a>
<a href="paging.jsp?disPage=<%=String.valueOf(allPageCount)%>">末页</a>
<%
}
else if(currentPage!=allPageCount)
{
%>
<a href="paging.jsp?disPage=<%=String.valueOf(originalPage)%>">首页</a>
<a href="paging.jsp?disPage=<%=String.valueOf(currentPage-1)%>">上一页</a>
<a href="paging.jsp?disPage=<%=String.valueOf(currentPage+1)%>">下一页</a>
<a href="paging.jsp?disPage=<%=String.valueOf(allPageCount)%>">末页</a>
<%
}
else{
%>
<a href="paging.jsp?disPage=+<%= originalPage%>">首页</a>
<a href="paging.jsp?disPage=<%=(currentPage-1) %>">上一页</a>
<%
}
%>
</td>
</tr>
</table>
</body>
</html>
|