package
pagination_tag;
import java.sql. * ;
import java.io.Serializable;
import javax.servlet.jsp.tagext.TagSupport;
import java.sql.ResultSetMetaData;
import java.util. * ;
public class Pagination
extends TagSupport implements Serializable {
public Pagination() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private Connection con;
private Statement stm;
private ResultSet rs;
private ResultSetMetaData metaData;
private int columnCount;
private String tableName;
private String driverName;
private String connString;
private int page;
private boolean f = true;
private ArrayList arrayList = new ArrayList();
private int isRowCount;
private int isPageCount;
private int pageSize;
public void setConnString(String connString) {
this.connString = connString;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int doStartTag() {
try {
//连接数据库
Class.forName(driverName);
con = DriverManager.getConnection(connString, "sa", "sa");
stm = con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stm.executeQuery("select * from " + tableName);
metaData = rs.getMetaData(); //记录集中的元数据
columnCount = metaData.getColumnCount(); //获得列数
rs.last();
isRowCount = rs.getRow(); //获得总行数
if (isRowCount % pageSize == 0) {
isPageCount = isRowCount / pageSize; //得到总页数
}
else {
//因为isRowCount和pageSize都为整数 所以结果只取整数部分 余下的小数部分的条数为单独一页,所以要加1
isPageCount = (isRowCount / pageSize + 1); //获得总行数
}
//获得参数
String pp = pageContext.getRequest().getParameter("p");
if (pp == null) {
page = 1;
}
else {
page = Integer.parseInt(pp);
}
/**//*
if (page > isPageCount) {
page = isPageCount;
}
if (page < 1) {
page = 1;
}
*/
如果不要下面的if语句,表格中将只会显示记录集中的最后一条记录。
if (page > 0) {
rs.absolute((page-1)*pageSize+1); //使游标定位在新的一页的第一条记录.
}
pageContext.getOut().print("当前页是第" + page + "页" +" 总页数为"+isPageCount+"<br>");
//显示数据的表
pageContext.getOut().print("<table width=80% border=1 bgColor=33ccff>");
pageContext.getOut().print("<tr>");
for (int i = 1; i <= columnCount; i++) {
pageContext.getOut().print("<td align=center>");
pageContext.getOut().print(metaData.getColumnName(i)); //打印字段名
pageContext.getOut().print("</td>");
}
pageContext.getOut().print("</tr>");
int z = 0;
//***************
//pageSize为每页的行数 while循环pageSize次后退出或者是记录集中最后一条记录时退出
while (pageSize > z && !rs.isAfterLast()) {
for (int i = 1; i <= columnCount; i++) {
pageContext.getOut().print("<td align=center>");
pageContext.getOut().print(rs.getString(i)); //打印出记录集中每一行的数据
pageContext.getOut().print("</td>");
}
rs.next();
z++;
pageContext.getOut().print("</tr>");
}
//******while**********
pageContext.getOut().print("</table>");
pageContext.getOut().print("<p align=center>");
if (page != 1) {
pageContext.getOut().print("       " +
"<a href=testPagination.jsp?p=" +
1 + ">第一页</a>" + "    ");
//p为所传的参数
pageContext.getOut().print("<a href=testPagination.jsp?p=" +
( (page) - 1) + ">上一页</a>" +
"   ");
}
if (page != isPageCount) {
pageContext.getOut().print("   " +
"<a href=testPagination.jsp?p=" +
( (page) + 1) + ">下一页</a>" +
"   ");
pageContext.getOut().print("<a href=testPagination.jsp?p=" +
isPageCount + ">最后一页</a>");
}
pageContext.getOut().print("</p>");
}
catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private void jbInit() throws Exception {
}
}
testPagination.jsp
import java.sql. * ;
import java.io.Serializable;
import javax.servlet.jsp.tagext.TagSupport;
import java.sql.ResultSetMetaData;
import java.util. * ;
public class Pagination
extends TagSupport implements Serializable {
public Pagination() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private Connection con;
private Statement stm;
private ResultSet rs;
private ResultSetMetaData metaData;
private int columnCount;
private String tableName;
private String driverName;
private String connString;
private int page;
private boolean f = true;
private ArrayList arrayList = new ArrayList();
private int isRowCount;
private int isPageCount;
private int pageSize;
public void setConnString(String connString) {
this.connString = connString;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int doStartTag() {
try {
//连接数据库
Class.forName(driverName);
con = DriverManager.getConnection(connString, "sa", "sa");
stm = con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stm.executeQuery("select * from " + tableName);
metaData = rs.getMetaData(); //记录集中的元数据
columnCount = metaData.getColumnCount(); //获得列数
rs.last();
isRowCount = rs.getRow(); //获得总行数
if (isRowCount % pageSize == 0) {
isPageCount = isRowCount / pageSize; //得到总页数
}
else {
//因为isRowCount和pageSize都为整数 所以结果只取整数部分 余下的小数部分的条数为单独一页,所以要加1
isPageCount = (isRowCount / pageSize + 1); //获得总行数
}
//获得参数
String pp = pageContext.getRequest().getParameter("p");
if (pp == null) {
page = 1;
}
else {
page = Integer.parseInt(pp);
}
/**//*
if (page > isPageCount) {
page = isPageCount;
}
if (page < 1) {
page = 1;
}
*/
如果不要下面的if语句,表格中将只会显示记录集中的最后一条记录。
if (page > 0) {
rs.absolute((page-1)*pageSize+1); //使游标定位在新的一页的第一条记录.
}
pageContext.getOut().print("当前页是第" + page + "页" +" 总页数为"+isPageCount+"<br>");
//显示数据的表
pageContext.getOut().print("<table width=80% border=1 bgColor=33ccff>");
pageContext.getOut().print("<tr>");
for (int i = 1; i <= columnCount; i++) {
pageContext.getOut().print("<td align=center>");
pageContext.getOut().print(metaData.getColumnName(i)); //打印字段名
pageContext.getOut().print("</td>");
}
pageContext.getOut().print("</tr>");
int z = 0;
//***************
//pageSize为每页的行数 while循环pageSize次后退出或者是记录集中最后一条记录时退出
while (pageSize > z && !rs.isAfterLast()) {
for (int i = 1; i <= columnCount; i++) {
pageContext.getOut().print("<td align=center>");
pageContext.getOut().print(rs.getString(i)); //打印出记录集中每一行的数据
pageContext.getOut().print("</td>");
}
rs.next();
z++;
pageContext.getOut().print("</tr>");
}
//******while**********
pageContext.getOut().print("</table>");
pageContext.getOut().print("<p align=center>");
if (page != 1) {
pageContext.getOut().print("       " +
"<a href=testPagination.jsp?p=" +
1 + ">第一页</a>" + "    ");
//p为所传的参数
pageContext.getOut().print("<a href=testPagination.jsp?p=" +
( (page) - 1) + ">上一页</a>" +
"   ");
}
if (page != isPageCount) {
pageContext.getOut().print("   " +
"<a href=testPagination.jsp?p=" +
( (page) + 1) + ">下一页</a>" +
"   ");
pageContext.getOut().print("<a href=testPagination.jsp?p=" +
isPageCount + ">最后一页</a>");
}
pageContext.getOut().print("</p>");
}
catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private void jbInit() throws Exception {
}
}
====================
<
%@page
contentType
="text/html; charset=GBK"
%
>
< %@taglib
prefix ="pagination"
uri ="/WEB-INF/taglibTest.tld" % >
< html >
< head >
< title > testPagination </ title >
</ head >
< body bgcolor ="#ffffff" >
< p align ="center" > < font size ="10" > 商品信息表 </ font ></ p >
< pagination:Display
driverName ="sun.jdbc.odbc.JdbcOdbcDriver"
connString ="jdbc:odbc:wangDBbase"
tableName ="studentDetail" pageSize ="3" />
</ body >
</ html >
taglibTest.tld
< %@taglib
prefix ="pagination"
uri ="/WEB-INF/taglibTest.tld" % >
< html >
< head >
< title > testPagination </ title >
</ head >
< body bgcolor ="#ffffff" >
< p align ="center" > < font size ="10" > 商品信息表 </ font ></ p >
< pagination:Display
driverName ="sun.jdbc.odbc.JdbcOdbcDriver"
connString ="jdbc:odbc:wangDBbase"
tableName ="studentDetail" pageSize ="3" />
</ body >
</ html >
============
<!
DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"
>
< taglib >
< tlibversion > 1.0 </ tlibversion >
< jspversion > 2.0 </ jspversion >
< shortname > pagination </ shortname >
< tag >
< name > Display </ name >
< tagclass > pagination_tag.Pagination </ tagclass >
< attribute >
< name > driverName </ name >
</ attribute >
< attribute >
< name > connString </ name >
</ attribute >
< attribute >
< name > tableName </ name >
</ attribute >
< attribute >
< name > pageSize </ name >
</ attribute >
</ tag >
</ taglib >
< taglib >
< tlibversion > 1.0 </ tlibversion >
< jspversion > 2.0 </ jspversion >
< shortname > pagination </ shortname >
< tag >
< name > Display </ name >
< tagclass > pagination_tag.Pagination </ tagclass >
< attribute >
< name > driverName </ name >
</ attribute >
< attribute >
< name > connString </ name >
</ attribute >
< attribute >
< name > tableName </ name >
</ attribute >
< attribute >
< name > pageSize </ name >
</ attribute >
</ tag >
</ taglib >