亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

javaWeb-分頁(yè)查詢

來(lái)源:放肆的小青年 發(fā)布時(shí)間:2019-04-19 15:49:09 閱讀量:1890

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;//起始頁(yè)

private int next;//下一頁(yè)

private int last;//上一頁(yè)

private int currentPage;//當(dāng)前頁(yè)

private int totalPage;//總頁(yè)數(shù)

private List studentList;//用戶信息

//構(gòu)造方法

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從零開(kāi)始因此要加1

//calculate the totalPage

totalPage = num % NUM == 0 ? num/NUM : num/NUM+1;//如果總記錄數(shù)不是NUM的倍數(shù),那么加1;例如21,那么總共有5頁(yè)

}

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中直接輸入占位符會(huì)報(bào)錯(cuò),所以就使用變量代替

        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);//一開(kāi)始查詢的時(shí)候,沒(méi)有start這個(gè)請(qǐng)求參數(shù),所以會(huì)為null

        }

        Page page = new Page();

        page.setStart(start);

        try {

            page = stuService.getPage(page);

            request.setAttribute("stus",page);//屬性設(shè)置

            request.getRequestDispatcher("student.jsp").forward(request,response);//轉(zhuǎn)發(fā)實(shí)現(xiàn)

        } 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


標(biāo)簽: PHP 環(huán)境搭建
分享:
評(píng)論:
你還沒(méi)有登錄,請(qǐng)先