?? 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;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -