?? jdbc分頁(yè)程序.txt
字號(hào):
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
/**
* Pager, 基于 JDBC 2.0 滾動(dòng)機(jī)制的分頁(yè)程序, 在 MySQL, SQLServer, Access, Oracle 下測(cè)試通過(guò).
* @author 劉長(zhǎng)炯
* @version 1.0 2004-8-12
*/
public class Pager {
/** Used database connection */
Connection conn = null;
public Pager() {
}
/**
* 分頁(yè)功能, 返回當(dāng)頁(yè)的數(shù)據(jù)(JDBC 2.0 實(shí)現(xiàn)).
*
* @param currentPage
* 當(dāng)前頁(yè)面數(shù)(取值范圍: 從 1 開(kāi)始有效, 0 自動(dòng)改為 1)
* @param pageCount
* 每頁(yè)顯示記錄數(shù)
*
* @return a Vector - 數(shù)據(jù)列表
*/
public Vector pageData(int currentPage, int pageCount) {
Vector results = new Vector();
String tableName = "table_name";// 要處理的表格名
ResultSet rs = null;
String sql = "SELECT * FROM " + tableName;
Statement stmt = null;
try {
// TODO: open connection
// 生成可滾動(dòng)的結(jié)果集表達(dá)式
stmt = conn.createStatement(ResultSet.
TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
int count = recordCount(); // 總記錄數(shù)
int totalPage = (int) Math.ceil(1.0 * count / pageCount); // 總頁(yè)面數(shù)
if (currentPage <= 0) {
currentPage = 1;
}
// 超出頁(yè)碼范圍, 不返回?cái)?shù)據(jù)
if (currentPage > totalPage) {
currentPage = totalPage;
return results;
}
if ((currentPage - 1) * pageCount > 0) {
// 移動(dòng)結(jié)果集數(shù)據(jù)到當(dāng)前頁(yè)
rs.absolute((currentPage - 1) * pageCount);
}
// rs.absolute(0); 在 ODBC 下會(huì)導(dǎo)致如下異常:java.sql.SQLException: Cursor
// position (0) is invalid
int i = 0; // Readed pages
while (rs.next() && i < pageCount) {
i++;
// TODO: Read each row and process to value object
ValueObject bean = new ValueObject();
// TODO: Read value to value object
result.add(bean);
}
} catch (Exception exception) {
System.out.println("Occur a error in " + getClass()
+ ".pageData() : " + exception.getMessage());
// exception.printStackTrace();
} finally {
closeJDBCResource(stmt);
closeJDBCResource(rs);
closeJDBCResource(conn);
}
return results;
}
/**
* 返回當(dāng)前數(shù)據(jù)庫(kù)中記錄的總數(shù).
*
* @return int 記錄總數(shù)
*/
public int recordCount() {
int allCount = -1;
String tableName = "table_name";// 要處理的表格名
String sql = "SELECT COUNT(*) FROM " + tableName;
ResultSet rs = null;
Statement stmt = null;
try {
// TODO: open connection
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
allCount = rs.getInt(1);
}
} catch (Exception exception) {
System.out
.println("Occur a error in " + getClass()
+ ".recordCount() : " + exception.getMessage());
} finally {
closeJDBCResource(stmt);
closeJDBCResource(rs);
closeJDBCResource(conn);
}
return allCount;
}
/**
* Close a jdbc resource, such as ResultSet, Statement, Connection.... All
* these objects must have a method signature is void close().
*
* @param resource -
* jdbc resouce to close
*/
public static void closeJDBCResource(Object resource) {
try {
Class clazz = resource.getClass();
java.lang.reflect.Method method = clazz.getMethod("close", null);
method.invoke(resource, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Test page.
* @param args
*/
public static void main(String[] args) {
// 分頁(yè), 讀取第一頁(yè)數(shù)據(jù), 共讀取5個(gè)記錄
Vector data = new Pager().pageData(1, 5);
// TODO: process value object, 更改類名
for(int i = 0; results != null && i < data.size(); i++) {
ValueObject bean = (ValueObject)data.get(i);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -