【Java开源代码栏目提醒】:网学会员Java开源代码为您提供GdPageCachedRowSet.java参考,解决您在GdPageCachedRowSet.java学习中工作中的难题,参考学习。
package com.gd.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.CachedRowSet;
import com.gd.jdbc.impl.GdDbConnection;
import com.sun.rowset.CachedRowSetImpl;
public class GdPageCachedRowSet {
protected CachedRowSet crs = null;
protected int rowsCount;
protected int pageSize;
protected int curPage;
protected String command = "";
/**
* 获取下一笔记录
* @return
* @throws SQLException
*/
public boolean next() throws SQLException {
return crs.next();
}
/**
* 获取栏目名所对应的值
* @param columnName
* @return
* @throws SQLException
*/
public String getString(String columnName) throws SQLException {
try {
return crs.getString(columnName);
} catch (SQLException e) {
throw new SQLException(e.toString() + " columnName=" + columnName);
}
}
/**
* 返回当前页号
*/
public int getCurPage() {
return curPage;
}
/**
* 返回CachedRowSet
*/
public CachedRowSet getCachedRowSet() {
return crs;
}
/**
* 返回总页数
*/
public int getPageCount() {
if (rowsCount == 0)
return 0;
if (pageSize == 0)
return 1;
// calculate PageCount
double tmpD = (double) rowsCount / pageSize;
int tmpI = (int) tmpD;
if (tmpD > tmpI)
tmpI++;
return tmpI;
}
/**
* 返回当前页的记录条数
*/
public int getPageRowsCount() {
//没有分页,则返回总的数据行数
if (pageSize == 0)
return rowsCount;
//假如有分页,但没有数据,则返回0
if (getRowsCount() == 0)
return 0;
//假如当前页不等于最后一页,则返回当前页的记录数,即分页大小
if (curPage != getPageCount())
return pageSize;
//表示为最后一页时的记录数
return rowsCount - (getPageCount() - 1) * pageSize;
}
/**
* 返回分页大小
*/
public int getPageSize() {
return pageSize;
}
/**
* 返回总记录行数
*/
public int getRowsCount() {
return rowsCount;
}
/**
* 转到指定页
*/
public void gotoPage(int page) {
if (crs == null)
return;
if (page < 1)
page = 1;
if (page > getPageCount())
page = getPageCount();
//表示找到指定页在数据库中的开始位置的前一笔
int row = (page - 1) * pageSize;
try {
crs.absolute(row);
curPage = page;
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 转到当前页的第一条记录
* @exception java.sql.SQLException
*/
public void pageFirst() throws SQLException {
//表示找到指定页在数据库中的开始位置
int row = (curPage - 1) * pageSize + 1;
crs.absolute(row);
}
/**
* 转到当前页的最后一条记录
* @exception java.sql.SQLException
*/
public void pageLast() throws SQLException {
//表示找到指定页在数据库中的开始位置+当前页的记录数
int row = (curPage - 1) * pageSize + getPageRowsCount();
crs.absolute(row);
}
/**
* 设置分页大小
*/
public void setPageSize(int pageSize) {
if (pageSize >= 0) {
this.pageSize = pageSize;
curPage = 1;
}
}
/**
* 构造函数
*/
public GdPageCachedRowSet(Connection conn, HttpServletRequest request, String sql) throws SQLException {
this.crs = getCachedRowSet(conn, request, sql);
//表示没有得到结果集
if (crs == null)
throw new SQLException("没有得到结果集" + sql);
//得到结果集的总的记录数,并将其定位到第一个
if (this.crs.last()) {
rowsCount = this.crs.getRow();
this.crs.beforeFirst();
}
}
public CachedRowSet getCachedRowSet(Connection conn, HttpServletRequest request, String sql) throws SQLException {
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement(1004, 1007);
rs = stmt.executeQuery(sql);
crs = new CachedRowSetImpl();
crs.populate(rs);
return crs;
}
}
上一篇:
GdPage.java
下一篇:
国外高校后勤用工现状及存在问题研究University Logistics Research question the status quo and the presence of foreign l