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

📄 pagination.java

📁 一个用java开发的具有搜索功能的图书管理系统
💻 JAVA
字号:
package library;

import java.sql.SQLException;

public class Pagination {
	
	private int recordsPerPage=0;
	private int pageCount=0;
	private int currentPage=0;
	private int rowCount=-1;
	private int beginRow=0;				//本页记录开始行(包含)
	private int recordCount=0; 			//本页记录数

	public Pagination(){}

	/**
	 * @return currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}
	/**
	 * @return pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}
	/**
	 * @return recordsPerPage
	 */
	public int getRecordsPerPage() {
		return recordsPerPage;
	}
	/**
	 * @return rowCount
	 */
	public int getRowCount() {
		return rowCount;
	}
	/**
	 * @return beginRow
	 * @throws Exception 
	 */
	public int getBeginRow() throws Exception {
		if(validation()==true)
		{
			return beginRow;
		}
		else
		{
			return -1;
		}
	}
	/**
	 * @return recordCount
	 * @throws Exception 
	 */
	public int getRecordCount() throws Exception {
		if(validation()==true)
		{
			return recordCount;
		}
		else
		{
			return -1;
		}
	}
	/**
	 * @param currentPage 要设置的 currentPage
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		initialization();
		evaluation();
	}
	/**
	 * @param recordsPerPage 要设置的 recordsPerPage
	 * @throws SQLException 
	 */
	public void setRecordsPerPage(int recordsPerPage){
		this.recordsPerPage = recordsPerPage;
		initialization();
		evaluation();
	}
	/**
	 * @param rowCount 要设置的 rowCount
	 */
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
		initialization();
		evaluation();
	}
	private boolean validation() throws Exception
	{
		if(currentPage<=0)
		{
			throw new Exception("你还没有设置“currentPage(当前页面)”属性");
		}
		else
		{
			if(recordsPerPage<=0)
			{
				throw new Exception("你还没有设置“recordPerPage(每页记录数)”属性");
			}
			else
			{
				if(rowCount<0)
				{
					throw new Exception("你还没有设置“rowCount(记录数)”属性");
				}
				else
				{
					return true;
				}
			}
		}
	}
	private void evaluation()
	{
		if(pageCount>0&&currentPage>0&&rowCount>0&&recordsPerPage>0)
		{
			beginRow=(currentPage-1)*recordsPerPage+1;
			recordCount=(currentPage<pageCount)?recordsPerPage:(rowCount-1)%recordsPerPage+1;
		}
	}
	private void initialization()
	{
		pageCount=0;
		if(rowCount>0&&recordsPerPage>0)
		{
			pageCount=(rowCount-1)/recordsPerPage+1;
		}
//		if(resultSet!=null&&recordsPerPage>0)
//		{
//			ResultSetMetaData resultSetMetaData=resultSet.getMetaData();
//			columnCount=resultSetMetaData.getColumnCount();                    //得到字段数
//			resultSet.last();
//			rowCount=resultSet.getRow();                          				 //得到记录数
//			resultSet.beforeFirst();											//将光标移动到此 ResultSet 对象的开头,正好位于第一行之前。
//			String[][] tempRecords=new String[rowCount][columnCount];
//			int row=0;
//			while(resultSet.next())
//			{
//				for(int i=0;i<columnCount;i++)
//				{
//					tempRecords[row][i]=resultSet.getString(i+1);
//				}
//				row++;
//			}
//			pageCount=(rowCount-1)/recordsPerPage+1;
//			for(int i=0;i<rowCount;i++)
//			{
//				for(int j=0;j<columnCount;j++)
//				{
//					System.out.print(tempRecords[i][j]+"\t");
//				}
//				System.out.println();
//			}
//			records=tempRecords;
//		}
	}
//	public String[][] getCurrentPageResult() throws Exception
//	{
//		if(currentPage<=0)
//		{
//			throw new Exception("你还没有设置“currentPage(当前页面)”属性");
//		}
//		else
//		{
//			if(recordsPerPage<=0)
//			{
//				throw new Exception("你还没有设置“recordPerPage(每页记录数)”属性");
//			}
//			else
//			{
//				if(resultSet==null)
//				{
//					throw new Exception("你还没有设置“resultSet(记录集)”属性");
//				}
//				else
//				{
//					int count=(currentPage<pageCount)?recordsPerPage:(rowCount-1)%recordsPerPage+1;				//本页记录数
//					String[][] tempRecords=new String[count][columnCount];
//					for(int i=0;i<count;i++)
//					{
//						for(int j=0;j<columnCount;j++)
//						{
//							tempRecords[i][j]=records[(currentPage-1)*recordsPerPage][j];
//						}
//					}
//					return tempRecords;
//						
//				}
//			}
//		}
//	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -