文档库 最新最全的文档下载
当前位置:文档库 › 重写ResultSet实现分页功能(最好的分页技术)

重写ResultSet实现分页功能(最好的分页技术)

重写ResultSet实现分页功能(最好的分页技术)2007-07-17 23:44 重写ResultSet实现分页功能(最好的分页技术) 1.首先定义一个接口Pageable 继承ResultSet这个类

并在接口中定义一些自己的方法,具体方法如下:

package com.page;

import java.sql.ResultSet;

public interface Pageable extends ResultSet {


/**返回总页数
*/
int getPageCount();
/**返回当前页的记录条数
*/
int getPageRowsCount();
/**返回分页大小
*/
int getPageSize();
/**转到指定页
*/
void gotoPage(int page) ;
/**设置分页大小
*/
void setPageSize(int pageSize);
/**返回总记录行数
*/
int getRowsCount();
/**
* 转到当前页的第一条记录
* @exception java.sql.SQLException 异常说明。
*/
void pageFirst() throws java.sql.SQLException;
/**
* 转到当前页的最后一条记录
* @exception java.sql.SQLException 异常说明。
*/
void pageLast() throws java.sql.SQLException;
/**返回当前页号
*/
int getCurPage();



}



2.然后定义一个类PageableResultSet 实现这个接口,覆盖ResultSet的所有方法.并实现接口Pageable 中自定义的方法,具体实现方式如下:

package com.page;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import https://www.wendangku.net/doc/d48134854.html,.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Ref;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;

public class PageableResultSet implements Pageable {


protected java.sql.ResultSet rs=null;
protected int rowsCount;
protected int pageSize;
protected int curPage;
protected String command = "";


public PageableResultSet(java.sql.ResultSet rs) throws java.sql.SQLException {
if(rs==null) throw new SQLException("given ResultSet is NULL","user");
https://www.wendangku.net/doc/d48134854.html,st();
rowsCount=rs.getRow();
System.out.println(rowsCount);
rs.beforeFirst();
this.rs=rs;

}
public int getCurPage() {
return curPage;
}

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;
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 (rs == null)
return;
if (page < 1)
page = 1;
if (page > getPageCount())
page = getPageCount();
int row = (page - 1) * pageSize + 1;
try {
rs.absolute(row);
cu

rPage = page;
}
catch (java.sql.SQLException e) {
}


}

public void pageFirst() throws SQLException {
int row=(curPage-1)*pageSize+1;
rs.absolute(row);

}

public void pageLast() throws SQLException {
int row=(curPage-1)*pageSize+getPageRowsCount();
rs.absolute(row);


}

public void setPageSize(int pageSize) {
if(pageSize>=0){
this.pageSize=pageSize;
curPage=1;
}


}


public boolean next() throws SQLException {
return rs.next();
}

public boolean absolute(int row) throws SQLException {
return rs.absolute(row);
}

public void afterLast() throws SQLException {
rs.afterLast();

}

public void beforeFirst() throws SQLException {
rs.beforeFirst();

}

public void cancelRowUpdates() throws SQLException {
rs.cancelRowUpdates();
}

public void clearWarnings() throws SQLException {
rs.clearWarnings();

}

public void close() throws SQLException {
rs.close();

}

public void deleteRow() throws SQLException {
rs.deleteRow();

}

public int findColumn(String columnName) throws SQLException {
return rs.findColumn(columnName);
}

public boolean first() throws SQLException {
return rs.first();
}

public Array getArray(int i) throws SQLException {
return rs.getArray(i);
}

public Array getArray(String colName) throws SQLException {
return rs.getArray(colName);
}

public InputStream getAsciiStream(int columnIndex) throws SQLException {
return rs.getAsciiStream(columnIndex);
}

public InputStream getAsciiStream(String columnName) throws SQLException {
return rs.getAsciiStream(columnName);
}

public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
return rs.getBigDecimal(columnIndex);
}

public BigDecimal getBigDecimal(String columnName) throws SQLException {
return rs.getBigDecimal(columnName);
}

public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
return rs.getBigDecimal(columnIndex, scale);
}

public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException {
return rs.getBigDecimal(columnName, scale);
}

public InputStream getBinaryStream(int columnIndex) throws SQLException {
return rs.getBinaryStream(columnIndex);
}

public InputStream getBinaryStream(String columnName) throws SQLException {
return rs.getBinaryStream(columnName);
}

public Blob getBlob(int i) throws SQLException {
return rs.getBlob(i);
}

public Blob getBlob(String colName) throws SQLException {
return rs.getBlob(colName);
}

public boolean getBoolean(int columnIndex) throws SQLException {
return rs.getBoolean(columnIndex);
}

public boolean getBoolean(String columnName) throws SQLException {
return rs.getBoolean(columnName);
}

public byte getByte(int columnIndex) throws SQLException {
return rs.getByte(columnIndex);
}

public byte getByte(String columnName) throws SQLExce

ption {
// TODO 自动生成方法存根
return rs.getByte(columnName);
}

public byte[] getBytes(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getBytes(columnIndex);
}

public byte[] getBytes(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getBytes(columnName);
}

public Reader getCharacterStream(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getCharacterStream(columnIndex);
}

public Reader getCharacterStream(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getCharacterStream(columnName);
}

public Clob getClob(int i) throws SQLException {
// TODO 自动生成方法存根
return rs.getClob(i);
}

public Clob getClob(String colName) throws SQLException {
// TODO 自动生成方法存根
return rs.getClob(colName);
}

public int getConcurrency() throws SQLException {
// TODO 自动生成方法存根
return rs.getConcurrency();
}

public String getCursorName() throws SQLException {
// TODO 自动生成方法存根
return rs.getCursorName();
}

public Date getDate(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getDate(columnIndex);
}

public Date getDate(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getDate(columnName);
}

public Date getDate(int columnIndex, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.getDate(columnIndex, cal);
}

public Date getDate(String columnName, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.getDate(columnName, cal);
}

public double getDouble(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getDouble(columnIndex);
}

public double getDouble(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getDouble(columnName);
}

public int getFetchDirection() throws SQLException {
// TODO 自动生成方法存根
return rs.getFetchDirection();
}

public int getFetchSize() throws SQLException {
// TODO 自动生成方法存根
return rs.getFetchSize();
}

public float getFloat(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getFloat(columnIndex);
}

public float getFloat(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getFloat(columnName);
}

public int getInt(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getInt(columnIndex);
}

public int getInt(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getInt(columnName);
}

public long getLong(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getLo

ng(columnIndex);
}

public long getLong(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getLong(columnName);
}

public ResultSetMetaData getMetaData() throws SQLException {
// TODO 自动生成方法存根
return rs.getMetaData();
}

public Object getObject(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getObject(columnIndex);
}

public Object getObject(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getObject(columnName);
}

public Object getObject(int i, Map> map) throws SQLException {
// TODO 自动生成方法存根
return rs.getObject(i, map);
}

public Object getObject(String colName, Map> map) throws SQLException {
// TODO 自动生成方法存根
return rs.getObject(colName, map);
}

public Ref getRef(int i) throws SQLException {
// TODO 自动生成方法存根
return rs.getRef(i);
}

public Ref getRef(String colName) throws SQLException {
// TODO 自动生成方法存根
return rs.getRef(colName);
}

public int getRow() throws SQLException {
// TODO 自动生成方法存根
return rs.getRow();
}

public short getShort(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getShort(columnIndex);
}

public short getShort(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getShort(columnName);
}

public Statement getStatement() throws SQLException {
// TODO 自动生成方法存根
return rs.getStatement();
}

public String getString(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getString(columnIndex);
}

public String getString(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getString(columnName);
}

public Time getTime(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getTime(columnIndex);
}

public Time getTime(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getTime(columnName);
}

public Time getTime(int columnIndex, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.getTime(columnIndex, cal);
}

public Time getTime(String columnName, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.getTime(columnName, cal);
}

public Timestamp getTimestamp(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getTimestamp(columnIndex);
}

public Timestamp getTimestamp(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getTimestamp(columnName);
}

public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.get

Timestamp(columnIndex, cal);
}

public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
// TODO 自动生成方法存根
return rs.getTimestamp(columnName, cal);
}

public int getType() throws SQLException {
// TODO 自动生成方法存根
return rs.getType();
}

public URL getURL(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getURL(columnIndex);
}

public URL getURL(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getURL(columnName);
}

public InputStream getUnicodeStream(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
return rs.getUnicodeStream(columnIndex);
}

public InputStream getUnicodeStream(String columnName) throws SQLException {
// TODO 自动生成方法存根
return rs.getUnicodeStream(columnName);
}

public SQLWarning getWarnings() throws SQLException {
// TODO 自动生成方法存根
return rs.getWarnings();
}

public void insertRow() throws SQLException {
// TODO 自动生成方法存根
rs.insertRow();

}

public boolean isAfterLast() throws SQLException {
// TODO 自动生成方法存根
return rs.isAfterLast();
}

public boolean isBeforeFirst() throws SQLException {
// TODO 自动生成方法存根
return rs.isBeforeFirst();
}

public boolean isFirst() throws SQLException {
// TODO 自动生成方法存根
return rs.isFirst();
}

public boolean isLast() throws SQLException {
// TODO 自动生成方法存根
return rs.isLast();
}

public boolean last() throws SQLException {
// TODO 自动生成方法存根
return https://www.wendangku.net/doc/d48134854.html,st();
}

public void moveToCurrentRow() throws SQLException {
// TODO 自动生成方法存根
rs.moveToCurrentRow();
}

public void moveToInsertRow() throws SQLException {
// TODO 自动生成方法存根
rs.moveToInsertRow();
}

public boolean previous() throws SQLException {
// TODO 自动生成方法存根
return rs.previous();
}

public void refreshRow() throws SQLException {
// TODO 自动生成方法存根
rs.refreshRow();
}

public boolean relative(int rows) throws SQLException {
// TODO 自动生成方法存根
return rs.relative(rows);
}

public boolean rowDeleted() throws SQLException {
// TODO 自动生成方法存根
return rs.rowDeleted();
}

public boolean rowInserted() throws SQLException {
// TODO 自动生成方法存根
return rs.rowInserted();
}

public boolean rowUpdated() throws SQLException {
// TODO 自动生成方法存根
return rs.rowUpdated();
}

public void setFetchDirection(int direction) throws SQLException {
// TODO 自动生成方法存根
rs.setFetchDirection(direction);
}

public void setFetchSize(int rows) throws SQLException {
// TODO 自动生成方法存根
rs.setFetchSize(rows);
}

public void updat

eArray(int columnIndex, Array x) throws SQLException {
// TODO 自动生成方法存根
rs.updateArray(columnIndex, x);
}

public void updateArray(String columnName, Array x) throws SQLException {
// TODO 自动生成方法存根
rs.updateArray(columnName, x);
}

public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateAsciiStream(columnIndex, x, length);
}

public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateAsciiStream(columnName, x, length);
}

public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBigDecimal(columnIndex, x);
}

public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBigDecimal(columnName, x);
}

public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateBinaryStream(columnIndex, x, length);
}

public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateBinaryStream(columnName, x, length);
}

public void updateBlob(int columnIndex, Blob x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBlob(columnIndex, x);
}

public void updateBlob(String columnName, Blob x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBlob(columnName, x);
}

public void updateBoolean(int columnIndex, boolean x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBoolean(columnIndex, x);
}

public void updateBoolean(String columnName, boolean x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBoolean(columnName, x);
}

public void updateByte(int columnIndex, byte x) throws SQLException {
// TODO 自动生成方法存根
rs.updateByte(columnIndex, x);
}

public void updateByte(String columnName, byte x) throws SQLException {
// TODO 自动生成方法存根
rs.updateByte(columnName, x);
}

public void updateBytes(int columnIndex, byte[] x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBytes(columnIndex, x);
}

public void updateBytes(String columnName, byte[] x) throws SQLException {
// TODO 自动生成方法存根
rs.updateBytes(columnName, x);
}

public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateCharacterStream(columnIndex, x, length);
}

public void updateCharacterStream(String columnName, Reader reader, int length) throws SQLException {
// TODO 自动生成方法存根
rs.updateCharacterStream(columnName, reader, length);
}

public voi

d updateClob(int columnIndex, Clob x) throws SQLException {
// TODO 自动生成方法存根
rs.updateClob(columnIndex, x);
}

public void updateClob(String columnName, Clob x) throws SQLException {
// TODO 自动生成方法存根
rs.updateClob(columnName, x);
}

public void updateDate(int columnIndex, Date x) throws SQLException {
// TODO 自动生成方法存根
rs.updateDate(columnIndex, x);
}

public void updateDate(String columnName, Date x) throws SQLException {
// TODO 自动生成方法存根
rs.updateDate(columnName, x);
}

public void updateDouble(int columnIndex, double x) throws SQLException {
// TODO 自动生成方法存根
rs.updateDouble(columnIndex, x);
}

public void updateDouble(String columnName, double x) throws SQLException {
// TODO 自动生成方法存根
rs.updateDouble(columnName, x);
}

public void updateFloat(int columnIndex, float x) throws SQLException {
// TODO 自动生成方法存根
rs.updateFloat(columnIndex, x);
}

public void updateFloat(String columnName, float x) throws SQLException {
// TODO 自动生成方法存根
rs.updateFloat(columnName, x);
}

public void updateInt(int columnIndex, int x) throws SQLException {
// TODO 自动生成方法存根
rs.updateInt(columnIndex, x);
}

public void updateInt(String columnName, int x) throws SQLException {
// TODO 自动生成方法存根
rs.updateInt(columnName, x);
}

public void updateLong(int columnIndex, long x) throws SQLException {
// TODO 自动生成方法存根
rs.updateLong(columnIndex, x);
}

public void updateLong(String columnName, long x) throws SQLException {
// TODO 自动生成方法存根
rs.updateLong(columnName, x);
}

public void updateNull(int columnIndex) throws SQLException {
// TODO 自动生成方法存根
rs.updateNull(columnIndex);
}

public void updateNull(String columnName) throws SQLException {
// TODO 自动生成方法存根
rs.updateNull(columnName);
}

public void updateObject(int columnIndex, Object x) throws SQLException {
// TODO 自动生成方法存根
rs.updateObject(columnIndex, x);
}

public void updateObject(String columnName, Object x) throws SQLException {
// TODO 自动生成方法存根
rs.updateObject(columnName, x);
}

public void updateObject(int columnIndex, Object x, int scale) throws SQLException {
// TODO 自动生成方法存根
rs.updateObject(columnIndex, x, scale);
}

public void updateObject(String columnName, Object x, int scale) throws SQLException {
// TODO 自动生成方法存根
rs.updateObject(columnName, x, scale);
}

public void updateRef(int columnIndex, Ref x) throws SQLException {
// TODO 自动生成方法存根
rs.updateRef(columnIndex, x);
}

public void updateRef(String columnName, Ref x) throws SQLException {
// TODO 自动生成方法存根
rs.updateRef(columnName, x)

;
}

public void updateRow() throws SQLException {
// TODO 自动生成方法存根
rs.updateRow();
}

public void updateShort(int columnIndex, short x) throws SQLException {
// TODO 自动生成方法存根
rs.updateShort(columnIndex, x);
}

public void updateShort(String columnName, short x) throws SQLException {
// TODO 自动生成方法存根
rs.updateShort(columnName, x);
}

public void updateString(int columnIndex, String x) throws SQLException {
// TODO 自动生成方法存根
rs.updateString(columnIndex, x);
}

public void updateString(String columnName, String x) throws SQLException {
// TODO 自动生成方法存根
rs.updateString(columnName, x);
}

public void updateTime(int columnIndex, Time x) throws SQLException {
// TODO 自动生成方法存根
rs.updateTime(columnIndex, x);
}

public void updateTime(String columnName, Time x) throws SQLException {
// TODO 自动生成方法存根
rs.updateTime(columnName, x);
}

public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
// TODO 自动生成方法存根
rs.updateTimestamp(columnIndex, x);
}

public void updateTimestamp(String columnName, Timestamp x) throws SQLException {
// TODO 自动生成方法存根
rs.updateTimestamp(columnName, x);
}

public boolean wasNull() throws SQLException {
// TODO 自动生成方法存根
return rs.wasNull();
}


}



3.如何运用

获得ResultSet的时候:

pstm=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//这里的属性一定要记得修改

Pageable rs=new PageableResultSet(pstm.executeQuery());//得到ResultSet

4.设置RS

rs.setPageSize(pageSize);//设置每页显示数目
rs.gotoPage(page);//设置当前选择第几页
for (int i = 0; i < rs.getPageRowsCount(); i++) {
/...进行取值.../
rs.next();//不要忘记next()
}




相关文档