package com.page.entity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.page.utils.DBConnection;
public class PageObjectLike {
private int countPage=0;//js count属性,页数
private int indexPage=0;//js start属性,当前页
private int showRows=10;//每页显示多少行数据
private int totalRows=0;//总共多少条数据
private boolean hasPrevPage=false;//是否有上一页
private boolean hasNextPage=false;//是否有下一页
List<SimRecords> list=new ArrayList<SimRecords>();//当前查询页的数据集合
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public int getIndexPage() {
return indexPage;
}
public void setIndexPage(int indexPage) {
this.indexPage = indexPage;
}
public int getShowRows() {
return showRows;
}
public void setShowRows(int showRows) {
this.showRows = showRows;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public boolean isHasPrevPage() {
return hasPrevPage;
}
public void setHasPrevPage(boolean hasPrevPage) {
this.hasPrevPage = hasPrevPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public List<SimRecords> getList() {
return list;
}
public void setList(List<SimRecords> list) {
this.list = list;
}
/**
* 记录分页的状态
* @param page
*/
public PageObjectLike getPageStateObject(int page) {
if(page>this.countPage){
page=this.countPage;
}
this.hasNextPage=(page<this.countPage)?true:false;
this.hasPrevPage=(page-1!=0)?true:false;
this.indexPage=page;
return this;
}
//////////////////////////允许条件查询和分页行数改变//////////////////////////
/**
* 返回分页模糊查询页数据
*/
public List<SimRecords> searchLikeResult(int page,String param){
Connection conn=DBConnection.getConnection();
String sql="select id,terminal_code,sim_balance,sim_car_no,save_time FROM simserverrecord where sim_balance between ? and ? limit "+showRows+" offset "+(page-1)*showRows;
PreparedStatement pst=null;
try {
pst =conn.prepareStatement(sql);
String params[]=param.split(",");
pst.setString(1, params[0]);
pst.setString(2, params[1]);
ResultSet rs=pst.executeQuery();
SimRecords sim=null;
while (rs.next()) {
sim=new SimRecords(rs.getInt("id"), rs.getString("terminal_code"), rs.getDouble("sim_balance"), rs.getString("sim_car_no"), rs.getDate("save_time"));
this.list.add(sim);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 计算模糊查询的所有数据行
* @param param
* @return
*/
public int rowsCountLike(String param){
Connection conn=DBConnection.getConnection();
String sql="select count(*) num from simserverrecord where sim_balance between ? and ?";
PreparedStatement pst=null;
this.totalRows=0;
try {
pst =conn.prepareStatement(sql);
String params[]=param.split(",");
pst.setString(1, params[0]);
pst.setString(2, params[1]);
ResultSet rs=pst.executeQuery();
rs.next();
this.totalRows=rs.getInt("num");
return this.totalRows;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
/**
* 计算总页数
* @param allrows
* @return
*/
private int getCountPage(int allrows){
if(allrows>0){
int count=allrows/showRows;
countPage=((allrows%showRows)>0)?count+1:count;
return countPage;
}
return 0;
}
/**
* 封装查询分页对象
* @param page
* @param param
* @return
*/
public PageObjectLike getPageObjectLike(int page,String param){
PageObjectLike o=this;
int count=o.rowsCountLike(param);//查询总条数
o.getCountPage(count); //计算总页数
o.getPageStateObject(page);//更新分页状态
o.list=o.searchLikeResult(page,param);//查询余额大于等于参数的记录
return o;
}
public static void main(String[] args) {
PageObjectLike o=new PageObjectLike();
o.getPageObjectLike(1, "10,20");
System.out.println("共计:"+o.totalRows+"条, 每页"+o.showRows+"条,分为:"+o.countPage+"页。 ");
System.out.println("有上一页:"+o.hasPrevPage);
System.out.println("有下一页:"+o.hasNextPage);
}
}