java jtable 分页_Jtable分页查询完整实现

使用数据库:mysql

实现效果:

a60a57b445055fc2d574178fea197d6c.png

界面实现说明:

初始化时,默认显示为第一页,首页与上一页按钮会被禁用;

当前为第x页/共y页内容当中,x与y是动态计算设置的;

翻页到最后一页时,下一页与末页按钮会被禁用;

根据条件查询时,如果条件不被清空且查询结果超过一页,分页功能依然可用。

后台实现说明:

Page 类:该类用来计算总页数、当前页、设置当前为第x页/共y页内容等功能,代码如下:

package com.tcm.model;

import java.util.Map;

import javax.swing.JButton;

import javax.swing.JLabel;

import com.tcm.constant.ITcmConstant;

/**

*

*

application name: tcm

*

application describing: 分页实体类

*

copyright: Copyright © 2018 brian_xiong@outlook.com版权所有

*

company: xxxx

*

time: 2018年9月17日 - 上午11:51:25

*

* @author brian_xiong@outlook.com

* @version $Revision: 1.2 $

*/

public class Page {

/**

* 当前页

*/

private int currentPage;

/**

* 每页显示多少条

*/

private int pageSize;

/**

* 总记录数

*/

private int recordCount;

/**

* 总页数

*/

private int pageCount;

/**分页按钮组*/

/**

* 首页按钮组件

*/

private JButton firstPageBtn;

/**

* 上一页按钮组件

*/

private JButton prePageBtn;

/**

* 下页按钮组件

*/

private JButton nextPageBtn;

/**

* 末页按钮组件

*/

private JButton lastPageBtn;

/**

* 描述信息组件

*/

private JLabel pageDesc;

/**

* 页码描述

*/

private String pageDesContent = "当前为第currentPage页/共pageCont页";

/**

*

* {构造方法}

*

* @param currentPage 当前页

* @param pageSize 每页显示多少条

* @param recordCount 总记录数

* @param buttonGroup 翻页按钮组 包括table右下角的翻页情况描述,每次点击按钮时,都要调用 根据计算的值动态处理

* @author:brian_xiong

*/

public Page(int currentPage,int pageSize,int recordCount,Map buttonGroup) {

this.currentPage = currentPage;

this.pageSize = pageSize;

this.recordCount = recordCount;

//计算总页数

pageCount = (recordCount + pageSize - 1)/pageSize;

dealButtonGroup(buttonGroup);

}

/**

*

* {处理分页的按钮可用于禁用}

*

* @param buttonGroup

* @author:brian_xiong

*/

private void dealButtonGroup(Map buttonGroup) {

this.firstPageBtn = (JButton) buttonGroup.get(ITcmConstant.FIRSTPAGE);

this.prePageBtn = (JButton) buttonGroup.get(ITcmConstant.PREPAGE);

this.nextPageBtn = (JButton) buttonGroup.get(ITcmConstant.NEXTPAGE);

this.lastPageBtn = (JButton) buttonGroup.get(ITcmConstant.LASTPAGE);

this.pageDesc = (JLabel) buttonGroup.get(ITcmConstant.PAGEDESC);

dynamicSetPageDesc(pageDesc);

if(this.getCurrentPage() == 1) {//初始页面 为第一页 时禁用 首页按钮 与上一页按钮

if(!this.lastPageBtn.isEnabled()) {//当前页是末页,点击首页按钮

this.lastPageBtn.setEnabled(true);

}

this.firstPageBtn.setEnabled(false);

this.prePageBtn.setEnabled(false);

}

if(this.getCurrentPage() > 1) {// 如果在中间页 则 首页与上一页按钮可用

this.firstPageBtn.setEnabled(true);

this.prePageBtn.setEnabled(true);

}

if(this.getCurrentPage() == this.getPageCount()) {//如果当前页为最后一页 则禁用 末页 与下一页按钮

this.lastPageBtn.setEnabled(false);

this.nextPageBtn.setEnabled(false);

}

if(this.getCurrentPage() < this.getPageCount() && !this.lastPageBtn.isEnabled()) {

this.lastPageBtn.setEnabled(true);

this.nextPageBtn.setEnabled(true);

}

}

/**

*

* {动态设置页面 Jtable右}

*

* @param pageDesc

* @author:brian_xiong

*/

private void dynamicSetPageDesc(JLabel pageDesc) {

String desContent = pageDesContent;

desContent = desContent.replaceAll("currentPage",Integer.toString(this.getCurrentPage()));

desContent = desContent.replaceAll("pageCont",Integer.toString(this.getPageCount()));

pageDesc.setText(desContent);

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getRecordCount() {

return recordCount;

}

public void setRecordCount(int recordCount) {

this.recordCount = recordCount;

}

public int getPageCount() {

return pageCount;

}

public void setPageCount(int pageCount) {

this.pageCount = pageCount;

}

}

2.分页查询函数片段:

public ResultSet getTcmCradList(TcmCardInfo u,int pageNum,int pageSize) {

StringBuffer sb = new StringBuffer("select * from tcm_cardinfo where 1=1");

if(StringUtil.isNotEmpty(u.getCardNo())) {

sb.append(" and cardNo like '%" + u.getCardNo() + "%'");

}

if(StringUtil.isNotEmpty(u.getNetworkType())) {

sb.append(" and networkType like '%" + u.getNetworkType() + "%'");

}

if(StringUtil.isNotEmpty(u.getMisdn())) {

sb.append(" and misdn like '%" + u.getMisdn() + "%'");

}

sb.append(" limit "+(pageNum-1)*pageSize+","+pageSize+"");

try {

conn = DbUtil.getCon();

ps = conn.prepareStatement(sb.toString());

return ps.executeQuery();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

以上是分页查询实现的整个思路。不足之处欢迎指正。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值