1、JSP分页问题,我们先确定每页的显示的行数,再从数据库中读出总共的记录数。SQL语句:"count(*) from article where pid=0"。
2、然后根据此算出总共的页数,然后在显示每页时,从数据库从读出相应页面的记录。
SQL语句:"select * from article where pid = 0 order by pdate desc limit by startPol,PAGE_SIZE"
3、原则:(1) 当接受的页数为空,或小于等于零时,页面定位到第一页;(2) 计算总页数:计算总页数:总页数=(记录数+每页记录数-1)/每页记录数 (3)定位每页的指针,(页数-1)*每页记录数+1 。
4、一种好的分页方法(在有些数据库下可能不会成功):
int intPageSize;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i;
intPageSize = 6;
strPage= request.getParameter("page");
if(strPage==null){
intPage = 1;
}else{
intPage = Integer.parseInt(strPage);
}
if(intPage<1){
intPage = 1;
}
rs.last();
intRowCount = rs.getRow();//得到记录数
intPageCount = (intRowCount + intPageSize - 1)/intPageSize;//得到页数
if(intPage>intPageCount){
intPage = intPageCount;
}
if(intPageCount>0){
rs.absolute((intPage-1)*intPageSize+1);//记录指针定位
}
i = 0;
while(i<intPageSize && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString("username") %></td>
<td><%=rs.getString("mobile") %></td>
<td><%=rs.getString("phone") %></td>
<td><%=rs.getString("mail") %></td>
<td><%=rs.getDate("lastcontact") %></td>
<td><%=rs.getString("memo") %></td>
</tr>
<%
rs.next();
i++;
}