📄 pagination.java
字号:
package Bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import db.DBCon;
public class Pagination {
private String table="";//要得到分页的表名
private int menuId=-1;//得要分页的栏目ID
private String menuIds="";//得到栏目的所有子栏目
private int rows=0;//得到总记录的条数
private int cur=0;//当前页数
private int max_id=0;//当前最大ID
private int pagerows=15;//每页显示记录数
private int pagesnums=0;//总页数
private String term="";//sql语句条件
private String action="";
db.DBCon DC;
DealString dstr;
public Pagination(){}
public Pagination(String table,int cur,int menuId,String action){
this.table=table;
this.menuId=menuId;
this.cur=cur;
DC=DBCon.getInstance();
dstr=new DealString();
this.action=action;
initvariable();
}
private void initvariable(){//初始化一些变量
if(menuId>0 && !action.trim().equals("topic")){
this.menuIds=String.valueOf(menuId);
getmenuchild(menuId);
String[] mends=dstr.splitStr(menuIds, '|');
term="where (MenuID="+mends[0].trim();
for(int i=1;i<mends.length;i++){
term+=" or MenuID="+mends[i].trim();
}
term+=")";
}
String sql;
if(action.trim().equals("aduit")){//审核
if(!term.trim().equals("")){
sql="select count(*) as count from "+table+" "+term+" and IsDel='0' and IsShow='0'";
}else{
sql="select count(*) as count from "+table+" where IsDel='0' and IsShow='0'";
}
}else if(action.trim().equals("recycle")){//回收站
if(!term.trim().equals("")){
sql="select count(*) as count from "+table+" "+term+" and IsDel='1'";
}else{
sql="select count(*) as count from "+table+" where IsDel='1'";
}
}else if(action.trim().equals("topic")){//专题管理
if(menuId>0){
sql="select count(*) as count from "+table+" where IsDel='0' and TopicID like '%"+menuId+"%'";
}else{
sql="select count(*) as count from "+table+" where IsDel='0' and TopicID <>''";
}
}
else if(action.trim().equals("stage")){
sql="select count(*) as count from "+table+" "+term+" and IsDel='0' and IsShow='1'";
}else{
if(!term.trim().equals("")){
sql="select count(*) as count from "+table+" "+term+" and IsDel='0'";
}else{
sql="select count(*) as count from "+table+" where IsDel='0'";
}
}
Connection conn = DC.getConnection();
Statement stat = null;
ResultSet rs=null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
rows=rs.getInt("count");
if((rows/pagerows)<=(float)rows/(float)pagerows){
pagesnums=rows/pagerows+1;
}else{
pagesnums=rows/pagerows;
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {rs.close();
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//该SQL语句得到当前页一第一个ID
if(action.trim().equals("aduit")){
if(menuId>0)
sql="select Id from "+table+" "+term+" and IsDel='0' and IsShow='0' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
else sql="select Id from "+table+" where IsDel='0' and IsShow='0' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
}else if(action.trim().equals("recycle")){//回收站
if(menuId>0)
sql="select Id from "+table+" "+term+" and IsDel='1' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
else sql="select Id from "+table+" where IsDel='1' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
}else if(action.trim().equals("topic")){
if(menuId>0)sql="select Id from "+table+" where IsDel='0' and TopicID like '%"+menuId+"%' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
else sql="select Id from "+table+" where IsDel='0' and TopicID<>'' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
}else if(action.trim().equals("stage"))
sql="select Id from "+table+" "+term+" and IsDel='0' and IsShow='1' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
else if(menuId>0)
sql="select Id from "+table+" "+term+" and IsDel='0' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
else
sql="select Id from "+table+" where IsDel='0' order by Id desc limit "+String.valueOf((cur-1)*pagerows+1);
try {
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
max_id=rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {rs.close();stat.close();
} catch (SQLException e) {e.printStackTrace();
}
DC.freeConnection(conn);
}
}
private void getmenuchild(int menuid){//利用递归的方式得到子栏目
ResultSet rs=null;
Statement stat=null;
Connection conn=null;
String sql="select Id from menu where ParentID="+String.valueOf(menuid);
try{
conn=DC.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()){
menuIds+="|"+rs.getString("Id");
getmenuchild(rs.getInt("Id"));
}
}
catch(Exception ex){
System.out.print("在Pagination中得到子栏目ID时出错"+sql+ex);
}
finally{
try {rs.close();stat.close();
} catch (SQLException e) {e.printStackTrace();
}
DC.freeConnection(conn);
}
}
public ResultSet getArticleRs()
{
String sql;
if(action.trim().equals("aduit"))
{
if(menuId>0)
{
sql="select * from "+table+" "+term+" and Id<="+max_id+" and IsDel='0' and IsShow='0' order by Id desc limit "+pagerows;
}
else
{
sql="select * from "+table+" where Id<="+max_id+" and IsDel='0' and IsShow='0' order by Id desc limit "+pagerows;
}
}
else if(action.trim().equals("recycle"))//回收站
{
if(menuId>0)
{
sql="select * from "+table+" "+term+" and Id<="+max_id+" and IsDel='1' order by Id desc limit "+pagerows;
}
else
{
sql="select * from "+table+" where Id<="+max_id+" and IsDel='1' order by Id desc limit "+pagerows;
}
}
else if(action.trim().equals("topic"))//专题管理
{
if(menuId>0)
{
sql="select * from "+table+" where TopicID like '%"+menuId+"%' and Id<="+max_id+" and IsDel='0' order by Id desc limit "+pagerows;
}
else
{
sql="select * from "+table+" where TopicID <>'' and Id<="+max_id+" and IsDel='0' order by Id desc limit "+pagerows;
}
}
else if(action.trim().equals("stage"))
{
sql="select Id,Title,MenuID,IsImg,IsHead,IsGood,AddTime from "+table+" "+term+" and Id<="+max_id+" and IsDel='0' and IsShow='1' order by Id desc limit "+pagerows;
}
else
{
if(menuId>0)
{
sql="select * from "+table+" "+term+" and Id<="+max_id+" and IsDel='0' order by Id desc limit "+pagerows;
}
else
{
sql="select * from "+table+" where Id<="+max_id+" and IsDel='0' order by Id desc limit "+pagerows;
}
}
try{
Conn=DC.getConnection();
Stat = Conn.createStatement();
Rs = Stat.executeQuery(sql);
}catch(Exception ex){
System.out.print("在Pagination中得到子栏目ID时出错"+sql+ex);
}
return Rs;
}
ResultSet Rs=null;
Statement Stat=null;
Connection Conn=null;
public int getCur() {
return cur;
}
public void setCur(int cur) {
this.cur = cur;
}
public int getMenuId() {
return menuId;
}
public void setMenuId(int menuId) {
this.menuId = menuId;
}
public int getPagerows() {
return pagerows;
}
public void setPagerows(int pagerows) {
this.pagerows = pagerows;
}
public int getPagesnums() {
return pagesnums;
}
public void setPagesnums(int pagesnums) {
this.pagesnums = pagesnums;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void CloseConn(){
try {
if(Rs!=null)Rs.close();
if(Stat!=null)Stat.close();
} catch (SQLException e) {Rs=null;Stat=null;e.printStackTrace();
}
DC.freeConnection(Conn);
}
public int getMax_id() {
return max_id;
}
public void setMax_id(int max_id) {
this.max_id = max_id;
}
public String getMenuIds() {
return menuIds;
}
public void setMenuIds(String menuIds) {
this.menuIds = menuIds;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -