⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdpagecachedrowset.java

📁 j2ee程序。 spring、xml、 实现增加内容选项。
💻 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 + -