要实现分页首先要理解什么是当前页,首页,末页,以及其算法。
算法分析:
当前页:
int nowpage;
首页:
nowpage=1;
末页:
int countpage; 总页
nowpage=countpage;
上一页:
nowpage = nowpage-1;
if(nowpage<1){
nowpage=1;
}
下一页:
nowpage = nowpage+1;
if(nowpage>countpage){
nowpage=countpage;
}
理解概念:
当前页 nowpage
总页数 countpage =======7页
每页显示的记录数 10条
当前页开始的记录数:(nowpage-1)*10+1;
1 1-10
2 11-20
3 21 30
4 31 40
5 41 50
总记录数:
countrecord =64记录
总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
表的操作:
总记录数:select count(*) from 表名;
每页显示3条记录: 声明 int pagesize=3;
总页数:总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
当前页的记录信息:
select * from 表名 limit (nowpage-1)*pagesize,pagesize;
代码中方法如下:
//总记录数
Integer getCountRecord();
//当前页显示的记录
List<News> findNewPageInfo(Integer nowpage);
//显示所有记录
List<News> findAll();
//总页数
Integer getCountPage();
//算法实现
NewsService nService = new NewsServiceImpl();
List<News> entities = nService.findAll();
Integer countRecord = nService.getCountRecord();
Integer countPage = nService.getCountPage();
String npage = request.getParameter("nowpage");
int nowpage=1;
if("".equals(npage)||npage==null){
nowpage=1;
}else{
nowpage=Integer.parseInt(npage);
if(nowpage<=1){
nowpage=1;
}
if(nowpage>=countPage){
nowpage=countPage;
}
}
List<News> entitiesr = nService.findNewPageInfo(nowpage);
request.setAttribute("entities", entitiesr);
request.setAttribute("countRecord", countRecord);
request.setAttribute("countPage", countPage);
request.setAttribute("nowpage", nowpage);
request.getRequestDispatcher("./index.jsp").forward(request, response);
//JSP分页实现
<span><a href="${pageContext.request.contextPath}/list.do?nowpage=1">首页</span>
<span><a href="${pageContext.request.contextPath}/list.do?nowpage=${nowpage-1}">上一页</span>
<span><a href="${pageContext.request.contextPath}/list.do?nowpage=${nowpage+1}">下一页</span>
<span><a href="${pageContext.request.contextPath}/list.do?nowpage=${countPage}">末页</span>
<span>当前为第${nowpage}页,共${countRecord}条记录,共${countPage}页</span>
主要代码实例:
在包内创建NewsDaoImpl实现类并实现NewsDao接口:
package cn.csdn.web.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import cn.csdn.web.domain.News;
public class NewsDaoImpl implements NewsDao{
/*封装数据库操作的接口*/
private static Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
/*封装每页显示的记录数*/
private static final Integer PAGESIZE=10;
//总记录数
private Integer countRecord;
//总页数
public Integer countPage;
/* 声明URL地址 */
private static final String URL = "jdbc:mysql://localhost:3306/java3g?user=root&password=5211314&useUnicode=true&characterEncoding=UTF-8";
static {
try {
/* 第二步:加载驱动程序 */
Class.forName("com.mysql.jdbc.Driver");
/* 第三步:获取连接对象 */
conn = DriverManager.getConnection(URL);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<News> findNowPageInfo(Integer nowpage) {
List<News> allentities = new ArrayList<News>();
/* 第二步:获取连接对象 */
/* 第三步:定义sql语句 */
String sql = "select id,title,content from news limit ?,?";
try {
/* 第四步:根据sql语句创建预处理对象 */
pstmt = conn.prepareStatement(sql);
int index = 1;
pstmt.setInt(index++, (nowpage-1)*this.PAGESIZE);
pstmt.setInt(index++, this.PAGESIZE);
/* 第六步:执行查询 */
rs = pstmt.executeQuery();
/* 第七步:判断 */
while (rs.next()) {
/* 第五步:为占位符赋值 */
News entity = new News();
entity.setId(rs.getInt("id"));
entity.setTitle(rs.getString("title"));
entity.setContent(rs.getString("content"));
allentities.add(entity);
}
/* 第八步:释放资源 */
release(rs,pstmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return allentities;
}
public Integer findCountRecord() {
String sql="select count(*) from news";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
this.countRecord = rs.getInt(1);
}
release(rs, pstmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.countRecord;
}
public Integer findCountPage() {
findCountRecord();
this.countPage = this.countRecord%this.PAGESIZE==0?this.countRecord/this.PAGESIZE:this.countRecord/this.PAGESIZE+1;
return this.countPage;
}
//释放资源的方法
private void release(ResultSet rs, PreparedStatement pstmt) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (pstmt != null)
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Integer getContRecord() {
// TODO Auto-generated method stub
return null;
}
public Integer getCountPage() {
// TODO Auto-generated method stub
return null;
}
public List<News> findAll() {
// TODO Auto-generated method stub
return null;
}
}