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

📄 pagination.java

📁 关于使用oracle的分页,在java中
💻 JAVA
字号:
package com.jauan.util;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

import com.jauan.db.DBConnection;
/**
 *
 * @author xiaoming
 * 对oracle查询进行分页
 *
 */
public class Pagination {
 
 private int totalRow; //总记录数
 private int totalPage; //总页数
 private int currentPage = 1; //当前页码,从1计起
 private int numPerPage;  //每页记录数
 private DBConnection con;
 private String sql;      //查询语句
 private ResultSet rs;
 private PreparedStatement ps;
 public Pagination(String sql,int numPerpage){
  this.setSql(sql);
  this.setNumPerPage(numPerpage);
  con=new DBConnection();
  String totalSql = "select count(1) from (" + sql + ")";
	try {
		ps = con.prepareStatement(totalSql);
		rs=ps.executeQuery();
		if(rs.next()){
			this.setTotalRow(rs.getInt(1)); //设置总行数
		  }
	} catch (SQLException e) {
		
		e.printStackTrace();
	}
  this.setTotalPage(); //计算总页数
  
 }
  /**
   * 取得下一页内容
   * @return 下页内容的列表
   */
 public ResultSet  getNextPage(){
  
  if(currentPage > totalPage)
   return null;
  int startIndex = (currentPage-1) * numPerPage + 1;
  int endIndex = 0;
  if(currentPage == totalPage)
   endIndex = totalRow;
  else
   endIndex = currentPage * numPerPage;
  StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");               
  paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");               
  paginationSQL.append(sql);               
  paginationSQL.append("  ) temp where ROWNUM <= " + endIndex);               
  paginationSQL.append(" ) WHERE  num >= " + startIndex);
  try {
	ps=con.prepareStatement(paginationSQL.toString());
	rs =ps.executeQuery();

} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
  //currentPage++;
  return rs;
 }
 public int getCurrentPage() {
  return currentPage;
 }

 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }

 public int getNumPerPage() {
  return numPerPage;
 }

 public void setNumPerPage(int numPerPage) {
  this.numPerPage = numPerPage;
 }

 public int getTotalPage() {
  return totalPage;
 }

 public void setTotalPage() {
  
  if(totalRow%numPerPage == 0)
   totalPage = totalRow / numPerPage;
  else
   totalPage = totalRow / numPerPage + 1;
  
 }

 public int getTotalRow() {
  return totalRow;
 }

 public void setTotalRow(int totalRow) {
  this.totalRow = totalRow;
 }

 public String getSql() {
  return sql;
 }

 public void setSql(String sql) {
  this.sql = sql;
 }

}

⌨️ 快捷键说明

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