JDBC高效分页文档格式.docx
《JDBC高效分页文档格式.docx》由会员分享,可在线阅读,更多相关《JDBC高效分页文档格式.docx(16页珍藏版)》请在冰豆网上搜索。
);
if(nCurrentPage<
页数小于0:
(第"
+nCurrentPage+"
页)"
RecordSetset=newRecordSet();
strSQL=strSQL.trim();
ps=con.prepareStatement(strSQL,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//设置字段值
setFieldValue(ps,obj,strSQL);
//判断是否为查询SQL,还是更新SQL
if(strSQL.substring(0,strSQL.indexOf("
"
)).equalsIgnoreCase("
SELECT"
))
ResultSetrs=ps.executeQuery();
ResultSetMetaDatarsmd=rs.getMetaData();
intnColumn=rsmd.getColumnCount();
//移到结果集最后一条,取得记录总数
set.CURRENT_PAGE=nCurrentPage;
set.COMMON_PAGE_SIZE=nCommonPageSize;
if(nTotalRecordCount>
set.TOTAL_RECORD_COUNT=nTotalRecordCount;
else
rs.last();
set.TOTAL_RECORD_COUNT=rs.getRow();
}
set.TOTAL_PAGE=(set.TOTAL_RECORD_COUNT+nCommonPageSize-1)/nCommonPageSize;
if(nCurrentPage==set.TOTAL_PAGE&
&
set.TOTAL_RECORD_COUNT%nCommonPageSize!
set.CURRENT_PAGE_SIZE=set.TOTAL_RECORD_COUNT%nCommonPageSize;
set.CURRENT_PAGE_SIZE=nCommonPageSize;
if(set.TOTAL_RECORD_COUNT==0)
returnset;
//定位到当前页的页首
rs.absolute(nCommonPageSize*(nCurrentPage-1)+1);
do
Recordrecord=newRecord();
for(inti=0;
i<
nColumn;
i++)
StringstrField=rsmd.getColumnName(i+1).toUpperCase();
record.put(strField,rs.getObject(i+1));
set.add(record);
while(rs.getRow()<
nCommonPageSize*nCurrentPage&
rs.next());
rs.close();
set.TOTAL_RECORD_COUNT=ps.executeUpdate();
catch(Exceptione)
e.printStackTrace();
returnnull;
finally
if(ps!
=null)
ps.close();
catch(Exceptione){}
静态方法直接调用,返回RS
调用
Recordrecord=(Record)rs.get(i);
Stringcode=record.getString("
code"
取翻页信息
<
tdvalign="
middle"
align="
right"
>
每页<
%=rs.COMMON_PAGE_SIZE%>
行
共<
%=rs.TOTAL_RECORD_COUNT%>
第<
%=rs.CURRENT_PAGE%>
页
%=rs.TOTAL_PAGE%>
BR>
%if(rs.CURRENT_PAGE==1){out.print("
首页上一页"
}else{
%>
AHREF="
javascript:
gotoPage
(1)"
首页<
/A>
gotoPage(<
%=rs.CURRENT_PAGE-1%>
)"
上一页<
%}%>
%if(rs.CURRENT_PAGE==rs.TOTAL_PAGE){out.print("
下一页尾页"
%=rs.CURRENT_PAGE+1%>
下一页<
尾页<
转到第<
SELECTname="
jumpPage"
onchange="
Jumping()"
<
%for(inti=1;
=rs.TOTAL_PAGE;
i++)
if(i==rs.CURRENT_PAGE){
OPTIONselectedvalue=<
%=i%>
/OPTION>
%}else{%>
OPTIONvalue=<
%}}%>
/SELECT>
/td>
//////////////////////////////////RecordSet.java///////////////////////////////////////
importjava.util.*;
publicclassRecordSet
extendsArrayList{
//记录集信息
/**总页数*/
publicintTOTAL_PAGE=-1;
/**当前页号*/
publicintCURRENT_PAGE=-1;
/**每页最大记录数*/
publicintCOMMON_PAGE_SIZE=-1;
/**当前页所含记录数*/
publicintCURRENT_PAGE_SIZE=-1;
/**总记录数*/
publicintTOTAL_RECORD_COUNT=-1;
/**当前指向的记录位置(初始位置在第一条记录之前的空位上)*/
privateintcurrentRecordRow=0;
/**
*取得当前记录的位置
*@return记录的位置
publicintgetRow(){
returncurrentRecordRow;
*得到第n条记录
*@parami记录位置(取值范围:
1--返回的记录数)
*@return成功返回记录,异常或失败返回false
publicRecordgetRecord(inti){
try{
return(Record)this.get(i-1);
catch(Exceptione){
//Log.error(e);
*得到当前记录
publicRecordgetRecord(){
if(isBeforeFirst()){
//Log.warn("
指针在初始位置,请使用first()或next()方法将指针指向第一条记录"
if(isAfterLast()){
指针在结束位置,请使用first()方法将指针指向第一条记录"
returngetRecord(currentRecordRow);
*定位到绝对位置的记录
*@paramrow记录位置(0--返回的记录数+1)
*@return成功返回true,异常或失败返回false
publicbooleanabsolute(introw){
if(0<
=row&
row<
=this.size()+1){
currentRecordRow=row;
returntrue;
else{
returnfalse;
*定位到首条记录之前
publicvoidbeforeFirst(){
currentRecordRow=0;
*定位到末条记录之后
publicvoidafterLast(){
currentRecordRow=this.size()+1;
*定位到首条记录
*@return成功返回true,失败返回false
publicbooleanfirst(){
if(this.isEmpty()){
currentRecordRow=1;
*定位到末条记录
publicbooleanlast(){
currentRecordRow=this.size();
*是否在首条记录之前
*@return是返回true,否返回false
publicbooleanisBeforeFirst(){
if(currentRecordRow==0){
*是否在末条记录之后
publicbooleanisAfterLast(){
if(currentRecordRow==this.size()+1){
*是否位于首条记录
publicbooleanisFirst(){
if(currentRecordRow==1){
*是否位于末条记录
publicbooleanisLast(){
if(currentRecordRow==this.size()){
*定位到前一条记录
publicbooleanprevious(){
if(currentRecordRow<
1){
currentRecordRow--;
*定位到后一条记录
publicbooleannext(){
if(currentRecordRow>
this.size()){
currentRecordRow++;
*得到数字(推荐使用这个方法得到数字,可以避免各种数据库数据类型不同而产生的问题)
*@paramkey字段名
*@return数字
publicdoublegetNumber(Stringkey){
returnDouble.parseDouble(getString(key));
*得到String类型的值(用getObject方法取得,并使用了trim方法去掉两端空格,当对象为空时返回空字符串)
*@returnString类型的值
publicStringgetString(Stringkey){
Objectobj=this.getRecord().getObject(key);
if(obj==null){
return"
"
;
returnobj.toString().trim();
*得到Timestamp类型的值
*@returnTimestamp类型的值
publicjava.sql.TimestampgetTimestamp(Stringkey){
returnthis.getRecord().getTimestamp(key);
*得到Date类型的值
*@returnDate类型的值
publicjava.sql.DategetDate(Stringkey){
returnthis.getRecord().getDate(key);
*得到Time类型的值
*@returnTime类型的值
publicjava.sql.TimegetTime(Stringkey){
returnthis.getRecord().getTime(key);
*得到BigDecimal类型的值
*@returnBigDecimal类型的值
publicjava.math.BigDecimalgetBigDecimal(Stringkey){
returnthis.getRecord().getBigDecimal(key);
*得到long类型的值
*@returnlong类型的值
publiclonggetLong(Stringkey){
returnthis.getRecord().getLong(key).longValue();
*得到int类型的值
*@returnint类型的值
publicintgetInt(Stringkey){
returnthis.getRecord().getInteger(key).intValue();
*得到short类型的值
*@returnshort类型的值
publicshortgetShort(Stringkey){
returnthis.getRecord().getShort(key).shortValue();
*得到double类型的值
*@returndouble类型的值
publicdoublegetDouble(Stringkey){
returnthis.getRecord().getDouble(key).doubleValue();
*得到float类型的值
*@returnfloat类型的值
publicfloatgetFloat(Stringkey){
returnthis.getRecord().getFloa