📄 pagination.java~1~
字号:
package scout.database.util;
import java.sql.*;
import java.util.*;
/**
* <p>Title: 国家陆生野生动物疫源疫病监测地理信息系统</p>
*
* <p>Description:数据库分页技术 </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: Chongqing Kemeida Corporation</p>
*
* @author ZhiHuaDeng
* @version 1.0
*/
public class Pagination {
/** 当前是第几页 */
protected int curPage = 1;
/** 一共多少页 */
protected int maxPage;
/** 一共多少行 */
protected int maxRowCount;
/** 每页多少行 */
public int rowsPerPage = 15;
/** 连接对象 */
public Connection con = null;
/** 操作的表对象 */
protected String tableName;
public Vector data;
public Pagination() {
}
public int getCurPage() {
return curPage;
}
public int getMaxPage() {
return maxPage;
}
public int getMaxRowCount() {
return maxRowCount;
}
public Vector getData() {
return data;
}
public String getTableName() {
return tableName;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
}
public void setData(Vector data) {
this.data = data;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
/**
* 得到要显示在本页的数据
* 输入参数:String page 第几页的内容,String tableName,操作的表名
* 返回类型:Pagination,可通过此对象的data属性访问结果。
**/
public Pagination getResult(String page) throws Exception {
try {
Pagination pagination = new Pagination();
pagination.setPageBean(this.tableName);
Vector v = new Vector();
int pageNum = Integer.parseInt(page); //第几页的数据。
Statement s = con.createStatement();
String sql = "select * from " + pagination.tableName + " LIMIT " +
((pageNum - 1) * pagination.rowsPerPage) + "," +
pagination.rowsPerPage;
ResultSet rs = s.executeQuery(sql);
v = this.getRecord(rs);
rs.close();
s.close();
pagination.setCurPage(pageNum);
pagination.data = v;
return pagination;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
/**
* 根据不同的表结构获得不同的结果(需要重写该方法)
* 输入参数:String page 第几页的内容,String tableName,操作的表名
* 返回类型:Pagination,可通过此对象的data属性访问结果。
**/
protected Vector getRecord(ResultSet rs) throws Exception {
Vector v = new Vector();
while (rs.next()) {
Object[] obj = new Object[3];
obj[0] = rs.getString(1);
obj[1] = rs.getString(2);
obj[2] = rs.getString(3);
v.add(obj);
}
return v;
}
/**
* 获得有效行数
* 输入类型:
* 输出类型:int
* */
protected int getAvailableCount() throws Exception {
int ret = 0;
ConnDataBase db = new ConnDataBase();
this.con = db.getConnection();
Statement s = con.createStatement();
String sql = "select count(*) from " + this.tableName;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
ret = rs.getInt(1);
}
return ret;
}
/**
* 设置PageBean*/
protected void setPageBean(String tableName) throws Exception {
//设置操作的表对象
this.setTableName(tableName);
//设置最大行数
this.setMaxRowCount(this.getAvailableCount());
if (this.maxRowCount % rowsPerPage == 0) {
//根据总行数计算总页数
this.maxPage = this.maxRowCount / rowsPerPage;
} else {
this.maxPage = this.maxRowCount / rowsPerPage + 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -