DBUtils Example 2 Implementing Paging Query

Keywords: Database Java SQL Apache

Requirements: For multiple pieces of data queried from the database, display the number of pages on one page
Function: Improve user experience
Such as:
[Previous Page] 12 3 4 5 6 7 [Next Page] Which Page/Total Pages

mysql database paging:
	   Selec... from XXX limit n, m (n is the index of the query, m queries several)		
	Page 1: limit 0.3
	Page 2: limit 3 3
	Page 3: limit 63
	Page n: (current page - 1) * m, M
		
Paging data required:
	1. The content displayed on the page select * from product limit (current page - 1) * m, M
	2. The number of items displayed on each page is specified by itself.
	3. The current page is passed from the front desk
	4. Total number of items select count(*) from product  
	5. The total number of pages equals the total number of pages/the number of pages displayed is rounded up
	
Background implementation:
Encapsulated as PageBean
	1.List data; // page display content select * from product limit n,m
	2.int pageSize; //Number of items displayed per page
	3.int pageNumber // Current page passed from the front desk
	4.int total // total bar number select count(*) from product 
	5.int pageTotal // Total pages (int)Math.ceil(total*1.0/pageSize)

Example: Paging query using PageBean
Pages show emp table employee name ename and line number, each page shows 5, default display the first page.
The last line shows the current page, the total number of pages.
The user enters n and displays the data on the next page. If it is the last page, the user is prompted to "be the last page".
Enter p and display the data on the previous page. If it is the first page, the user is prompted that "it is already the first page".
If
Interface examples:
1 zs1
2 ls1
3 ww1
4 zs2
5 ls2
Page 1 consists of 3 pages
Enter n, display the data on the next page, enter p, display the data on the previous page

package cn.njit.entity;

public class Emp {
	private String empno;
	private String ename;
	private String job;
	private String mgr;
	private float sal;
	private String deptno;
	public String getEmpno() {
		return empno;
	}
	public void setEmpno(String empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getMgr() {
		return mgr;
	}
	public void setMgr(String mgr) {
		this.mgr = mgr;
	}
	public float getSal() {
		return sal;
	}
	public void setSal(float sal) {
		this.sal = sal;
	}
	public String getDeptno() {
		return deptno;
	}
	public void setDeptno(String deptno) {
		this.deptno = deptno;
	}
	public String toString() {
		return empno+"\t"+ename+"\t"+sal;
	}
}

-----------------------------------------------------------------------------
package cn.njit.entity;

import java.util.List;

public class PageBean<T> {
	private List<T> data;
	private int pageSize;//Number of items displayed per page
	private int total;//Total number of records under current query conditions
	private int pageNumber;//Which page to display
	
	public int getPageCount() {
		return (int) Math.ceil(total*1.0/pageSize);
	}
	public List<T> getData() {
		return data;
	}
	public void setData(List<T> data) {
		this.data = data;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
}
------------------------------------------------------------------
package cn.njit.entity;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import util.C3P0Utils;

public class TestFenye {

	public static void main(String[] args) {
		PageBean<Emp> page = new PageBean<Emp>();
		int count = 0;
		page.setPageNumber(1 + count);
		page.setPageSize(5);
		testPageBean(page);

		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		boolean tip = true;
		while (tip) {
			if (str.equals("n")) {
				if (1 + count >= page.getPageCount()) {
					count = page.getPageCount() - 1;
					System.out.println("It's on the last page.");
				} else {
					count++;
				}

			} else if (str.equals("p")) {
				if (1 + count <= 1) {
					count = 0;
					System.out.println("Already on page one");
				} else {
					count--;
				}

			} else {
				System.out.println("Please re-enter");
			}

			page.setPageNumber(1 + count);
			page.setPageSize(5);
			testPageBean(page);
			str = sc.next();
		}

	}

	static void testPageBean(PageBean<Emp> page) {
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		ScalarHandler<Long> sch = new ScalarHandler<Long>();
		String str = "select count(1) from emp";
		try {
			Long cnt = qr.query(str, sch);
			page.setTotal(cnt.intValue());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		String sql = "select * from emp limit ?,?;";
		int pageSize = page.getPageSize();
		int begin = (page.getPageNumber() - 1) * pageSize;
		Object[] o = { begin, pageSize };
		BeanListHandler<Emp> bhs = new BeanListHandler<Emp>(Emp.class);
		try {
			List<Emp> list = qr.query(sql, bhs, o);
			page.setData(list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		int count = 0;

		for (Object e : page.getData()) {
			count++;
			System.out.println(count + "\t" + e.toString());
		}
		System.out.println("The first" + page.getPageNumber() + "page;common" + page.getPageCount() + "Page.");
		System.out.println("input n Display the next page,p Return to the previous page");
	}
}


Posted by JasonL on Thu, 31 Jan 2019 14:15:15 -0800