📄 resultsetcontroller.java
字号:
package com.alumni.bean;
import java.sql.*;
public class ResultSetController
{
private Connection conn;
private ResultSet rs;
private Statement stmt;
private String accessURL;
private String queryString;
private int currentPageNum;//当前页
private int rowsPerPage;//每页显示的数据行数
private int totalPages;//总页数
private int totalCounts;//总记录数
private boolean isEmpty;//是否有数据
private int count;//每页记录的记数器
//初始化参数(构造器)
public ResultSetController()
{
this.conn=null;
this.rs=null;
this.stmt=null;
this.accessURL="";
this.queryString="";
this.currentPageNum=1;
this.rowsPerPage=10;
this.totalPages=0;
this.totalCounts=0;
this.isEmpty=true;
this.count=0;
}
//运行主方法
public void run() throws SQLException
{
//连接到ACCESS数据库
if(this.conn==null)
{
this.connectToAccess(this.accessURL);
}
//初始化数据
this.initData();
//设定指针的位置
this.moveToCurrentPage(currentPageNum);
}
//初始化数据
private void initData() throws SQLException
{
this.stmt=this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
this.rs=this.stmt.executeQuery(this.queryString);
//计算总记录数
while(this.rs.next())
{
this.totalCounts++;
}
this.rs.beforeFirst();
//判断数据是否为空
if(this.totalCounts==0)
{
this.isEmpty=true;
this.totalCounts=0;
//默认时总页数为1页
this.totalPages=1;
}
else
{
this.isEmpty=false;
//计算总页数
if((this.totalCounts%this.rowsPerPage)==0)
{
this.totalPages=this.totalCounts/this.rowsPerPage;
}
else
{
this.totalPages=(this.totalCounts/this.rowsPerPage)+1;
}
}
}
//连接到ACCESS数据库
private void connectToAccess(String accessURL)
{
try
{
DBConnection dbc=new DBConnection();
this.conn=dbc.getConnectionToAccess(this.accessURL);
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
//判断是否有数据
public boolean isEmpty()
{
return this.isEmpty;
}
//根据设定的每页显示的记录数判断是否能继续输出数据
public boolean isAvailable() throws SQLException
{
if(this.rs.next()&&this.count++<this.rowsPerPage)
{
return true;
}
else
{
count=0;//计数器归零
return false;
}
}
//是否是数据顶部
public boolean isBeforeFirst() throws SQLException
{
return this.rs.isBeforeFirst();
}
//是否是数据底部
public boolean isAfterLast() throws SQLException
{
return this.rs.isAfterLast();
}
//是否是第一条数据
public boolean isFirst() throws SQLException
{
return this.rs.isFirst();
}
//是否是最后一条数据
public boolean isLast() throws SQLException
{
return this.rs.isLast();
}
//抓住数据库连接对象
public void setConnectionObject(Connection conn)
{
this.conn=conn;
}
//获得数据库连接对象
public Connection getConnectionObject()
{
return this.conn;
}
//设定数据库名
public void setAccessURL(String accessURL)
{
this.accessURL=accessURL;
}
//设定查询语句
public void setQueryString(String qs)
{
this.queryString=qs;
}
//设定当前显示的页面
public void setCurrentPageNum(int num)
{
this.currentPageNum=num;
}
//获得当前显示的页码
public int getCurrentPageNum()
{
return this.currentPageNum;
}
//设定每页可显示的数据行数
public void setRowsPerPage(int rows)
{
this.rowsPerPage=rows;
}
//(按指定的索引)取得当前指针位置的数据
public String getData(int column) throws SQLException
{
return this.rs.getString(column)!=null?this.rs.getString(column):"N/A";
}
//(按指定的列名)取得当前指针位置的数据
public String getData(String columnName) throws SQLException
{
return this.rs.getString(columnName)!=null?this.rs.getString(columnName):"N/A";
}
//获得当前每页可显示的数据行数
public int getRowsPerPage()
{
return this.rowsPerPage;
}
//获得总页数
public int getTotalPages()
{
return this.totalPages;
}
//获得总记录数
public int getTotalCounts()
{
return this.totalCounts;
}
//移动指针到指定的页面
public void moveToCurrentPage(int pageNum) throws SQLException
{
for(int i=1;i<this.currentPageNum;i++)
{
for(int j=0;j<this.rowsPerPage;j++)
{
this.moveToNext();
}
}
}
//把指针移动到下一条记录
public boolean moveToNext() throws SQLException
{
return this.rs.next();
}
//把指针移动到前一条记录
public boolean moveToPrevious() throws SQLException
{
return this.rs.previous();
}
//把指针移动到第一条记录
public boolean moveToFirst() throws SQLException
{
return this.rs.first();
}
//把指针移动到最后一条记录
public boolean moveToLast() throws SQLException
{
return this.rs.last();
}
//恢复指针到原始位置
public void moveToTop() throws SQLException
{
this.rs.beforeFirst();
}
//把指针移动到最底部
public void moveToBottom() throws SQLException
{
this.rs.afterLast();
}
//结束运行
public void disconnect() throws Throwable
{
if(this.conn!=null)
{
this.conn.close();
}
}
//当WEB服务器停止服务时回收资源
protected void finalize() throws Throwable
{
this.disconnect();
if(this.rs!=null)
this.rs.close();
if(this.stmt!=null)
this.stmt.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -