📄 pagebean.java
字号:
package org.lenovoAC.tools;
import java.sql.*;
import java.util.*;
import sun.jdbc.rowset.CachedRowSet;
/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class PageBean
{
private int totalPage;
private int curPage = 1;
private int nextPage;
private int perPage = 10; //每一页的分页数目
private int previousPage;
private int firstPage;
private int lastPage;
private String sql;
private String orderBy = "";
private int count;
public int getTotalPage()
{
return totalPage;
}
public void setCurPage(int curPage)
{
this.curPage = curPage;
}
public int getCurPage()
{
return curPage;
}
public int getNextPage()
{
if (getCurPage() == this.totalPage)
{
return this.totalPage; //到了最后面
}
else
{
return this.getCurPage() + 1;
}
}
public void setPerPage(int perPage)
{
this.perPage = perPage;
}
public int getPerPage()
{
return perPage;
}
public int getPreviousPage()
{
if (this.getCurPage() == 1)
{
return 1; //到了最前面
}
else
{
return this.getCurPage() - 1;
}
}
public void inital()
{
int i = 0;
try
{
DB mydb = new DB();
String query = "select count(*) num from (" + this.sql + ")";
CachedRowSet rs = mydb.executeQuery(query);
//System.out.println(query);
rs.next();
this.setCount(rs.getInt("num"));
//System.out.println(count);
if (rs.getInt("num") % this.getPerPage() == 0)
{
i = rs.getInt("num") / this.getPerPage();
}
else
{
i = rs.getInt("num") / this.getPerPage() + 1;
}
// System.out.println("total num:" + rs.getInt("num"));
if (rs.getInt("num") == 0)
{
i = 1;
}
this.totalPage = i;
this.firstPage = 1;
this.lastPage = i;
if (this.curPage > this.totalPage)
{
this.curPage = 1;
// System.out.println("modify curPage");
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public CachedRowSet getRecord()
throws Exception
{
CachedRowSet rs = null;
CachedRowSet cst = new CachedRowSet(); //
DB mydb = new DB();
try
{
int i = curPage - 1;
String query = null;
query =
"SELECT * FROM ( SELECT A.*, rownum r FROM ("
+ sql
+ ") A WHERE rownum <= "
+ this.perPage * this.curPage
+ " ) B WHERE r > "
+ this.perPage * (this.curPage - 1);
//System.out.println("在pagebean类加执行getRecord方法时所执行的SQL为:"+query);
rs = mydb.executeQuery(query);
cst.populate(rs); //
}
catch (Exception e)
{
// throw e;
System.out.println("执行pagebean.getRedcord方法时出错:\n" + e);
}
return cst; //
}
public CachedRowSet getRecord(String sql, int curpage, int perpage)
throws Exception
{
CachedRowSet rs = null;
CachedRowSet cst = new CachedRowSet(); //
DB mydb = new DB();
try
{
int i = curPage - 1;
String query = null;
query =
"SELECT * FROM ( SELECT A.*, rownum r FROM ("
+ sql
+ ") A WHERE rownum <= "
+ perpage * curpage
+ " ) B WHERE r > "
+ perpage * (curpage - 1);
// System.out.println(query);
rs = mydb.executeQuery(query);
cst.populate(rs); //
}
catch (Exception e)
{
throw e;
//System.out.println(e.getMessage());
}
return cst; //
}
/**
public CachedRowSet getRecord() {
OpenDbBean db = new OpenDbBean();
Connection conn = null;
ResultSet rs = null;
CachedRowSet crs = null;
Statement sqlStmt = null;
try {
crs = new CachedRowSet();
conn = db.getConnection();
sqlStmt =
conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
//执行SQL语句并获取结果集
rs = sqlStmt.executeQuery(this.sql);
if (this.totalPage > 0) {
//将记录指针定位到待显示页的第一条记录上
if (this.curPage > 1) {
rs.absolute((this.curPage - 1) * this.perPage);
}
crs.populate(rs);
}
return crs;
}
catch (SQLException ex) {
System.out.println(ex.getMessage());
return null;
}
finally {
try {
db.CleanConnection(conn, sqlStmt, rs);
}
catch (SQLException ex1) {
}
}
}
**/
public int getFirstPage()
{
return firstPage;
}
public void setFirstPage(int firstPage)
{
this.firstPage = firstPage;
}
public int getLastPage()
{
return lastPage;
}
public void setLastPage(int lastPage)
{
this.lastPage = lastPage;
}
public String getSql()
{
return sql;
}
public void setSql(String sql)
{
this.sql = sql;
}
public String getOrderBy()
{
return orderBy;
}
public void setOrderBy(String orderBy)
{
this.orderBy = orderBy;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -