如何用java做分页查询页面

版权声明:ApassionBoy https://blog.csdn.net/weixin_43150581/article/details/82587496

分页算法

  • 我们先在util中写一个分页的工具类,代码如下:
package com.util;

import java.util.ArrayList;

public class PageModel<T> {


//定义属性表示当前页
private int pageNo =1;
//定义属性表示每页显示的条数
private int pageSize =2;
//定义数学表示总记录数
private int count;
//定义集合存放每页显示的数据
private ArrayList<T> all;

/**
* 表示首页
* @return
*/
public int getIndex(){
return 1;
}

/**
* 上一页
* @return
*/
public int getPagePre(){
int pagePre = pageNo-1;
if(pagePre<1){
pagePre = pageNo;
}
return pagePre;
}

/**
* 最后一页
* @return
*/
public int getPageTotal(){
int pageTotal = 0;
if(count%pageSize==0){
pageTotal = count/pageSize;
}else{
pageTotal = count/pageSize+1;
}
return pageTotal;
}

/**
* 下一页
* @return
*/
public int getPageNext(){
int pageNext = pageNo+1;
if(pageNext>this.getPageTotal()){
pageNext = this.getPageTotal();
}
return pageNext;
}

public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ArrayList<T> getAll() {
return all;
}
public void setAll(ArrayList<T> all) {
this.all = all;
}


public PageModel(int pageNo, int pageSize, int count, ArrayList<T> all) {
super();
this.pageNo = pageNo;
this.pageSize = pageSize;
this.count = count;
this.all = all;
}


public PageModel() {
super();
}

}

  • 我们创建一个页面showStuInfo.jsp查询页面
  • 我在这提供了两种方法:方法一
  • 这里的第二种方法我就简单说:把showStuInfo.jsp页面的select换成用name来定义一个名称,然后直接在serlvet中取到pageSize的值,得先用<form>提交数据到servlet中,所以还要做一个提交按钮<input type="submit" value="提交"/>, 这里要用session获取到servlet中的值,再用增强for循环查出来,
    还要用
<script>的方法document.getElementById("Id").value取到select标签的值value,在传入servlet中

查看图1

//下面这里是是使用的是onclick触发事件当鼠标点击下拉框中的值的时候会把值提交到
<sclript>里面去执行show()方法,通过取到select  id的值来取到页面<option>的值,在通过location.href="";把值传给serlvet中

查看图2

  • 下面是servlet包中类方法
package com.servlet;


import java.io.IOException;
import java.util.ArrayList;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.dao.StuInfoDao;
import com.entity.StuInfo;
import com.util.PageModel;


public class StuInfoServlet extends HttpServlet {


//创建Dao类对象
StuInfoDao dao = new StuInfoDao();

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


this.doPost(request, response);
}



public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


//处理中文乱码
request.setCharacterEncoding("UTF-8");
//得到用户的操作
String op = request.getParameter("op");
//判断是否是查询所有学生信息
if("queryAll".equals(op)){
this.queryAllStuInfo(request, response);
}
}

/**
* 分页查询所有数据
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void queryAllStuInfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


//定义变量表示当前页
int pageNo =1;
int pageSize=1;
//接收页码
String pageNos = request.getParameter("pageNo");
//接收记录数
String pageSiz=request.getParameter("pageSize");
if(pageNos!=null&&pageNos!=""){
pageNo = Integer.parseInt(pageNos);
}
if(pageSiz!=null&&pageSiz!=""){
pageSize=Integer.parseInt(pageSiz);
}
//调用方法查询所有学生信息
PageModel<StuInfo> pageModel = dao.select(pageNo,pageSize);
//创建Session对象
HttpSession session=request.getSession();
session.setAttribute("pageModel", pageModel);
//request.setAttribute("pageModel", pageModel);
//跳转到查询页面
request.getRequestDispatcher("../showStuInfo.jsp").forward(request, response);
}


}


第三步是dao包中的方法:

package com.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.entity.StuInfo;
import com.util.DBUtil;
import com.util.PageModel;


public class StuInfoDao {
//常用的接口
ResultSet rs = null;
PreparedStatement ptmt = null;
Connection conn=null;

//创建工具类对象
DBUtil db = new DBUtil();
/**
* 查询的方法
* 分页查询
* @return
*/
public PageModel<StuInfo> select(int pageNo,int pageSize){
PageModel<StuInfo> model = new PageModel<StuInfo>();
ArrayList<StuInfo> list = new ArrayList<StuInfo>();
//调用方法链接数据库
conn = db.getConn();
try {
String sql = "select top "+pageSize+" * from stuInfo where stuId not in(select top (("+pageNo+"-1)*"+pageSize+") stuId from stuInfo)";
ptmt=conn.prepareStatement(sql);
rs = ptmt.executeQuery();
while(rs.next()){
//创建实体类对象
StuInfo si = new StuInfo(rs.getInt("stuId"),rs.getString("stuName"),rs.getString("stuSex"),rs.getInt("stuAge"),rs.getString("stuAddr"));
list.add(si);
}
//封装数据
model.setPageNo(pageNo);
model.setPageSize(pageSize);
model.setAll(list);
model.setCount(this.count());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
db.getOver(rs, ptmt, conn);
}
return model;
}

/**
* 查询数据总条数
* @return
*/
public int count(){
int count = 0;
//调用方法链接数据库
conn = db.getConn();
try {
String sql = "select COUNT(*) from stuInfo";
ptmt=conn.prepareStatement(sql);
rs = ptmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
db.getOver(rs, ptmt, conn);
}
return count;
}
}

好了,如果你学会了,记得留言告诉小编一声喔

猜你喜欢

转载自blog.csdn.net/weixin_43150581/article/details/82587496