📄 pagination.java
字号:
package pagination;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
/**
* 分页对象。用来表示数据翻页显示时的页信息,包括页数据和分页信息!
*
* <p>Copyright: Copyright (c) 2002-2004</p>
*
* <p>Company: www.EchoChina.net</p>
*
* @author YuLimin
* @version 1.0
*/
public class Pagination implements java.io.Serializable
{
private Pagination()
{
}
/**
* 当前页号
*/
private int iCurPage = 1;
/**
* 每页记录数
*/
private int iMaxPageCount = 20;
/**
* 总记录数
*/
private int iTotal = 0;
/**
* 本页的数据(记录)
*/
private Collection collection = new ArrayList();
/**
* 用来传递页码信息的http参数名称
*/
public static final String strNumber = "tNumber";
/**
* 获得当前页数。
*
* @return 当前页数
*/
public int getCurPage()
{
return this.iCurPage;
}
/**
* 获得每页最大记录数。
*
* @return 每页最大记录数
*/
public int getMaxPageCount()
{
return this.iMaxPageCount;
}
/**
* 设置每页最大记录数。
*
* @param iMaxPageCount 每页最大记录数
*/
public void setMaxPageCount(int iMaxPageCount)
{
this.iMaxPageCount = iMaxPageCount;
}
/**
* 得到总页数,算法:总记录数除以每页的记录数,结果为整数,若还有余数,则需要加一。
*
* @return int
*/
public int getTotalPageCount()
{
if(this.iMaxPageCount == 0)
{
return 0;
}
//先求得整除的结果
int iTotalPageCount = this.getTotalRecordCount() / this.iMaxPageCount;
//再求得模运算的结果
int iTemp = this.getTotalRecordCount() % this.iMaxPageCount;
//若模运算的结果不为零,则总页数为整除的结果加上模运算的结果
if(iTemp > 0)
{
iTotalPageCount += 1;
}
return iTotalPageCount;
}
// /**
// * 用于向form里写参数的JavaScript,如有可能独立出来成.js则更好一些。
// *
// * @param form String
// * @return String
// */
// public String buildJS(String form)
// {
// StringBuffer buf = new StringBuffer();
// buf.append("<Script Language=\"JavaScript\">\n");
// buf.append("<!--\n");
// buf.append("function gotoNumber(number)\n");
// buf.append("{\n");
// buf.append("\tif(").append(form).append(".").append(strNumber).append("==null)\n");
// buf.append("\t{\n");
// buf.append("\t\t").append(form).append(".insertAdjacentHTML(\"AfterBegin\",\"<input type='hidden' id='").append(strNumber).append("' name='").append(strNumber).append("' value=\" + number + \">\");\n");
// buf.append("\t}\n");
// buf.append("\telse\n");
// buf.append("\t{\n");
// buf.append("\t\t").append(form).append(".").append(strNumber).append(".value=+number;\n");
// buf.append("\t}\n");
// buf.append("\t").append(form).append(".submit();\n");
// buf.append("\t").append(form).append(".").append(strNumber).append(".value = 1;\n");
// buf.append("}\n");
// buf.append("\n");
// buf.append("function callGoto(totalPageCount,tNumber)\n");
// buf.append("{\n");
// buf.append("\tif(typeof(document.all.tNumber) == 'undefined' || document.all.tNumber.value < 1)\n");
// buf.append("\t{\n");
// buf.append("\t\tdocument.all.tNumber.value = 1;\n");
// buf.append("\t}\n");
// buf.append("\telse if(document.all.tNumber.value > totalPageCount)\n");
// buf.append("\t{\n");
// buf.append("\t\tdocument.all.tNumber.value = totalPageCount;\n");
// buf.append("\t}\n");
// buf.append("\tgotoNumber(document.all.tNumber.value);\n");
// buf.append("}\n");
// buf.append("//-->\n");
// buf.append("</Script>\n");
// return buf.toString();
// }
/**
* 获得用于翻页的html代码。其中包含跳转页码信息的http参数名称是tNumber。
*
* @param form HTML中包含查询条件的form名称。没有条件也要有一个空的form。 在翻页时,会通过JAVASCRIPT调用此form的submit()方法,以便在翻页时同时提交查询条件。
* 例子:formPagination(查询条件在本页面),parent.Query.formPagin(查询条件在另一个帧内) 翻页控制块的样式类属性为“Pagination”(HTML中的class属性)。
* @return java.lang.String
*/
public String getPaginationHTMLCode(String form)
{
StringBuffer strbuf = new StringBuffer();
int totalPageCount = getTotalPageCount();
//页码的html代码
final String strBlank = " ";
strbuf.append("<span class=\"Pagination\">");
strbuf.append("共").append(this.getTotalRecordCount()).append("条").append(strBlank);
strbuf.append("第").append(this.getCurPage()).append("/").append(totalPageCount).append("页").append(strBlank);
if(this.getCurPage() == 1 || this.getMaxPageCount() == 0)
{
strbuf.append("首页").append(strBlank).append("上页");
}
else
{
strbuf.append("<a href=\"JavaScript:gotoNumber(1);\">首页</a>").append(strBlank).append("<a href=\"JavaScript:gotoNumber(").append(getCurPage() - 1).append(");\">上页</a>");
}
strbuf.append(strBlank);
if(this.getCurPage() == totalPageCount || this.getMaxPageCount() == 0)
{
strbuf.append("下页").append(strBlank).append("尾页");
}
else
{
strbuf.append("<a href=\"JavaScript:gotoNumber(").append(getCurPage() + 1).append(");\">下页</a>").append(strBlank).append("<a href=\"JavaScript:gotoNumber(").append(totalPageCount).append(");\">尾页</a>");
}
strbuf.append(strBlank);
strbuf.append("<input name=\"tNumber\" size=\"3\" value=\"").append(this.getCurPage()).append("\">");
strbuf.append("<input type=\"button\" value=\"转到此页\" onclick=\"JavaScript:callGoto('" + totalPageCount + "',tNumber)\">");
strbuf.append("</span>");
return strbuf.toString();
}
/**
* 获得记录总数
*
* @return 记录总数
*/
public int getTotalRecordCount()
{
return this.iTotal;
}
/**
* 设置记录总数
*
* @param i int
*/
public void setTotalRecordCount(int i)
{
this.iTotal = i;
}
/**
* 从request对象获得当前的页码值。实际使用的是其中名为"tNumber"的参数,当这个参数没有赋值时,取页码的默认值。
*
* @param req 用来设置当前页码的request对象
* @throws PaginationException
* @return int
*/
public static int getCurPage(HttpServletRequest req) throws PaginationException
{
int i = 1;
String str = req.getParameter(strNumber);
if((str != null) && (str.length() > 0))
{
try
{
i = Integer.parseInt(str);
}
catch(NumberFormatException e)
{
throw new PaginationException("页码参数不正确");
}
}
return i;
}
/**
* 获取记录集合。
*
* @return Collection
*/
public Collection getData()
{
return collection;
}
public void setData(Collection collection)
{
this.collection = collection;
}
/**
* 设置当前页数。
*
* @param iCurPage int
* @throws PaginationException
*/
public void setCurrPageNO(int iCurPage) throws PaginationException
{
this.iCurPage = iCurPage;
}
/**
* 获得指定的页对象。
* @param strCountSQL 计算总记录数的sql语句。
* @param strSQL 查询记录的sql语句。
* @param conn 数据库连接。
* @param dao 业务对象生成工厂对象。会把由strCountSQL生成的ResultSet传给dao的getResultSetData方法。
* @param iMaxPageCount 每页最大记录数
* @param request request对象(HttpServletRequest)
* @return 页对象
* @throws PaginationException
*/
public static Pagination getPage(String strCountSQL,String strSQL,Connection conn,PaginationDAO dao,int iMaxPageCount,HttpServletRequest request) throws PaginationException
{
return getPage(strCountSQL,strSQL,conn,dao,iMaxPageCount,getCurPage(request));
}
/**
* 获得指定的页对象。
* @param strCountSQL 计算总记录数的sql语句。
* @param strSQL 查询记录的sql语句。
* @param conn 数据库连接。
* @param dao 业务对象生成工厂对象。会把由strCountSQL生成的ResultSet传给dao的getResultSetData方法。
* @param iMaxPageCount 每页最大记录数
* @param iCurPageNo 当前页数
* @return 页对象
* @throws PaginationException
*/
public static Pagination getPage(String strCountSQL,String strSQL,Connection conn,PaginationDAO dao,int iMaxPageCount,int iCurPageNo) throws PaginationException
{
Pagination pagination = new Pagination(); //页对象
Collection collection = new ArrayList(); //页中的数据
PreparedStatement ps = null;
ResultSet rs = null;
try
{
//获得总记录数
System.out.println("strCountSQL:" + strCountSQL);
System.out.println("strSQL:" + strSQL);
ps = conn.prepareStatement(strCountSQL);
rs = ps.executeQuery();
if((rs != null) && rs.next())
{
pagination.setTotalRecordCount(rs.getInt(1));
}
rs.close();
ps.close();
//获得记录
ps = conn.prepareStatement(strSQL,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = ps.executeQuery();
//将记录定位到当前页的位置。第一页不用定位。
if(iCurPageNo > 1)
{
rs.absolute(((iCurPageNo - 1) * iMaxPageCount));
}
for(int i = 0;i < iMaxPageCount;i++)
{
if(rs.next())
{
collection.add(dao.getResultSetData(rs));
}
else
{
if(i == 0)
{
//没有记录的情况
pagination.setCurrPageNO(0);
pagination.setMaxPageCount(0);
return pagination;
}
break;
}
}
}
catch(SQLException e)
{
e.printStackTrace();
throw new PaginationException(e.getMessage());
}
finally
{
try
{
if(rs != null)
{
rs.close();
}
if(ps != null)
{
ps.close();
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
pagination.setData(collection);
pagination.setCurrPageNO(iCurPageNo);
pagination.setMaxPageCount(iMaxPageCount);
return pagination;
}
/**
* 测试函数
*
* @param args String[]
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
Connection conn = null;
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/Pagination?useUnicode=true&characterEncoding=GB2312","root","password");
PaginationDAO dao = new PaginationDAO()
{
public Object getResultSetData(ResultSet rs) throws PaginationException
{
Vector v = new Vector();
try
{
v.add(rs.getString(1));
v.add(rs.getString(2));
v.add(rs.getString(3));
v.add(rs.getString(4));
}
catch(SQLException ex)
{
ex.printStackTrace();
throw new PaginationException(ex.getMessage());
}
return v;
}
};
Pagination p = Pagination.getPage("select count(*) from PersonInfo","select * from PersonInfo",conn,dao,5,3);
System.out.println(p.getPaginationHTMLCode("formPagination"));
Iterator itr = p.getData().iterator();
int count = 0;
while(itr.hasNext())
{
Vector v = (Vector)itr.next();
count++;
System.out.print("\n" + count);
for(int i = 0;i < v.size();i++)
{
System.out.print("\t" + (String)v.elementAt(i));
}
}
conn.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -