javaWeb-分页查询

来源:放肆的小青年 发布时间:2019-04-19 15:49:09 阅读量:1469

package com.stu;

 

public class Student {

private int id;

private String firstName;

private String lastName;

private String address;

private String phone;

 

public Student(){}

 

public Student(int id,String firstName,String lastName,String address,String phone){

    this.id = id;

    this.firstName = firstName;

    this.lastName = lastName;

    this.address = address;

    this.phone = phone;

}

 

public int getId() {

return id;

}

 

public void setId(int id) {

this.id = id;

}

 

public String getFirstName() {

return firstName;

}

 

public void setFirstName(String firstName) {

this.firstName = firstName;

}

 

public String getLastName() {

return lastName;

}

 

public void setLastName(String lastName) {

this.lastName = lastName;

}

 

public String getAddress() {

return address;

}

 

public void setAddress(String address) {

this.address = address;

}

 

public String getPhone() {

return phone;

}

 

public void setPhone(String phone) {

this.phone = phone;

}

 

@Override

public String toString() {

 return "Student{" +

                "id=" + id +

                ", firstName='" + firstName + '\'' +

                ", lastName='" + lastName + '\'' +

                ", address='" + address + '\'' +

                ", phone='" + phone + '\'' +

                '}';

 

}

}

 

package com.stu;

 

import java.util.List;

 

public class Page {

public final static int NUM = 5;

private int start;//起始页

private int next;//下一页

private int last;//上一页

private int currentPage;//当前页

private int totalPage;//总页数

private List studentList;//用户信息

//构造方法

public Page(){}

public Page(int start,int num,List studentList){

this.start = start;

this.studentList = studentList;

//caculate the last

last = start == 0? start:start-NUM;

// calculate the next

next = start + NUM > num ? start: start+NUM;

// calculate the currentPage

currentPage = start/NUM +1;//start从零开始因此要加1

//calculate the totalPage

totalPage = num % NUM == 0 ? num/NUM : num/NUM+1;//如果总记录数不是NUM的倍数,那么加1;例如21,那么总共有5页

}

public int getStart() {

return start;

}

public void setStart(int start) {

this.start = start;

}

public int getNext() {

return next;

}

public void setNext(int next) {

this.next = next;

}

public int getLast() {

return last;

}

public void setLast(int last) {

this.last = last;

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

public int getTotalPage() {

return totalPage;

}

public void setTotalPage(int totalPage) {

this.totalPage = totalPage;

}

public List getStudentList() {

return studentList;

}

public void setStudentList(List studentList) {

this.studentList = studentList;

}

 

}

 

package com.util;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

public class DBUtils {

private final static String DRIVER = “com.mysql.jdbc.Driver”;

private final static String URL =“jdbc:mysql://127.0.0.1:3306/exam”;

private final static String USERNAME=“root”;

private final static String PASSWORD=“root”;

 

    public static Connection getConnection() throws ClassNotFoundException, SQLException {

        Class.forName(DRIVER);

 

        Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);

 

        return conn;

    }

}

 

package com.dao;

 

import java.sql.SQLException;

import java.util.List;

 

import com.stu.Page;

import com.stu.Student;

 

public interface StudentDao {

public List getStudents(int start) throws SQLException, ClassNotFoundException;

public int getStudentsNum() throws SQLException, ClassNotFoundException;

public Page getPage(Page page)throws SQLException, ClassNotFoundException;

}

 

package com.dao.imp;

 

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 org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

import com.dao.StudentDao;

import com.stu.Page;

import com.stu.Student;

import com.util.DBUtils;

 

public class StudentDaoImp extends HibernateDaoSupport implements StudentDao {

private Connection conn;

private PreparedStatement pstmt ;

private ResultSet rs;

@Override

public List getStudents(int start) throws SQLException,

ClassNotFoundException {

List stuList = new ArrayList();

 

        conn = DBUtils.getConnection();

 

        String sql = "select * from student limit ?,?";

 

        pstmt = conn.prepareStatement(sql);

        int firstParameter = 1;//在Intellij中直接输入占位符会报错,所以就使用变量代替

        int secondParameter = 2;//

        pstmt.setInt(firstParameter, start);

        pstmt.setInt(secondParameter,Page.NUM);

 

        rs = pstmt.executeQuery();

        Student stu = null;

        while(rs.next()){

            int id = rs.getInt("id");

            String firstName = rs.getString("firstName");

            String lastName = rs.getString("lastName");

            String address = rs.getString("address");

            String phone = rs.getString("phone");

            stu = new Student(id,firstName,lastName,address,phone);

            stuList.add(stu);

        }

        if(rs != null) {

            rs.close();

        }

        if(pstmt != null){

            pstmt.close();

        }

        if(conn != null){

            conn.close();

        }

        return stuList;

 

}

 

@Override

public int getStudentsNum() throws SQLException, ClassNotFoundException {

    int num = 0;

        String sql = "select count(id) as count from student";

        conn = DBUtils.getConnection();

        pstmt = conn.prepareStatement(sql);

        rs = pstmt.executeQuery();

        while(rs.next()){

            num = rs.getInt("count");

        }

        return num;

 

}

 

@Override

public Page getPage(Page page) throws SQLException, ClassNotFoundException {

    int start = page.getStart();

        int num = getStudentsNum();

        List<Student> studentList = getStudents(start);

        Page p = new Page(start,num,studentList);

        return p;

}

 

package com.service;

 

import java.sql.SQLException;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.dao.StudentDao;

import com.stu.Page;

 

public class StudentService {

public StudentDao stuDao;

public StudentService(){

ApplicationContext context = new

ClassPathXmlApplicationContext(“applicationContext.xml”);

stuDao = (StudentDao) context.getBean(“stuDaoImp”);

}

public Page getPage(Page page) throws SQLException, ClassNotFoundException {

return stuDao.getPage(page);

}

 

}

 

package com.controller;

 

import java.io.IOException;

import java.sql.SQLException;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.service.StudentService;

import com.stu.Page;

 

public class StudentServlet extends HttpServlet {

private StudentService stuService = new StudentService();

 

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        doPost(req,res);

    }

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

        String startStr = request.getParameter("start");

        int start = 0;

        if(startStr != null){

            start = Integer.parseInt(startStr);//一开始查询的时候,没有start这个请求参数,所以会为null

        }

        Page page = new Page();

        page.setStart(start);

        try {

            page = stuService.getPage(page);

            request.setAttribute("stus",page);//属性设置

            request.getRequestDispatcher("student.jsp").forward(request,response);//转发实现

        } catch (SQLException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

    }

}

 

<?xml version="1.0" encoding="UTF-8"?> Student struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/classes/applicationContext.xml StudentServlet com.controller.StudentServlet StudentServlet /Student


标签: PHP 环境搭建
分享:
评论:
你还没有登录,请先