package com.bbs.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.struts2.ServletActionContext;
/**
* 分页类
*
* @author
*/
public class Page {
private Connection conn;
private PreparedStatement ps;
private ResultSet resultSet;
private String strPager=""; //分页代码
private String strPageName=""; //当前页面名称
private String parameter=""; //分页连接处所带的参数
private long totalNum = 0; //记录总数量
private long totalPages = 0; //总页数
private int page = 1; //当前页码
private int position; //游标位置
public int getPosition() {
return position;
}
public ResultSet paging(String sql,int pageSize) throws Exception{
//获取当前页
String strPage = ServletActionContext.getRequest().getParameter("page");
if (strPage != null&&strPage!="") {
page = Integer.parseInt(strPage);
}
//获取每页显示记录数
String strPageSize = ServletActionContext.getRequest().getParameter("pageSize");
if (strPageSize != null) {
pageSize = Integer.parseInt(strPageSize);
}
//取得数据
try {
DBUtils db=new DBUtils();
conn = db.getConnection();
ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// ResultSet.CONCUR_READ_ONLY 不能用结果集更新数据库中的表。
// ResultSet.TYPE_SCROLL_INSENSITIVE 结果集的游标可以上下移动,当数据库变化时,当前结果集不变
resultSet= ps.executeQuery();
resultSet.last();
//取得总记录数
totalNum = resultSet.getRow();
//计算分页页数
totalPages = (totalNum + pageSize - 1) / pageSize;
//游标定位
if(page!=1){
position = (page - 1) * pageSize;
resultSet.absolute(position);
}
else{
resultSet.beforeFirst();
}
} catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}
public String strPage()throws Exception{
this.strPager+="<div style='display:block;margin:5px;text-align:left;font-size:12px;'>" +
"<font color='black'>当前页:<b>"+this.page+"/"+this.totalPages+"</b> 页 数据总量 <b>"+totalNum+"</b></font></div>";
this.strPager+="<div style='display:block;margin-top:-25px;text-align:right;font-size:12px;'>";
this.strPager+=" <a href="+strPageName+"?page=1"+parameter+">首页</a>";
this.strPager+=" <a href="+strPageName+"?page="+(this.page>1?(page-1):1)+parameter+">上一页</a>";
this.strPager+=" <a href="+strPageName+"?page="+(this.page<totalPages?(this.page+1):totalPages)+parameter+">下一页</a>";
this.strPager+=" <a href="+strPageName+"?page="+totalPages+parameter+">尾页</a></div>";
return strPager;
}
public String strPage2()throws Exception{
this.strPager+="<strong><font size='2' color='#000000'>共<b>"+totalNum+"</b>条记录";
this.strPager+="<a href="+strPageName+"?page="+(this.page>1?(page-1):1)+parameter+"> <</a> ";
this.strPager+="<font color='black'><b>"+this.page+"</b> ";
this.strPager+="<a href="+strPageName+"?page="+(this.page<totalPages?(this.page+1):totalPages)+parameter+"> ></a> ";
this.strPager+="跳到第<input type='text' size='1' id='page' name='page'>页 <input type='button' value='GO' onclick='jump2()'/>";
this.strPager+="</font></strong>";
return strPager;
}
//<strong><font size="2" color="#000000">共条记录 < >跳到第<input type="text" size="1">页 <input type="button" value="GO"/>
// </font></strong>
/**设置页面的名字
*
*/
public void setStrPageName(String strPageName) {
this.strPageName = strPageName;
}
/**设置分页连接处所带的参数
*
*/
public void setParameter(String parameter) {
this.parameter = parameter;
}
}