使用JSP完成分页

本文讲解如何使用JSP页面进行数据分页。

1 下载mysql驱动包

下载mysql驱动包

2 创建表和插入测试数据

1)在MySQL的test数据库,使用以下SQL创建customer表:

CREATE TABLE `customer` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(50) DEFAULT NULL,
   `gender` varchar(20) DEFAULT NULL,
   `telephone` varchar(100) DEFAULT NULL,
   `address` varchar(100) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

注意:id采用MySQL的自增长策略,所以我们程序不用维护id值。

2)插入一些测试数据,如下:

3 编写Customer实体

package com.yiidian.domain;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class Customer {
    private Integer id;
    private String name;
    private String gender;
    private String telephone;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

4 编写CustomerDao

package com.yiidian.dao;

import com.yiidian.domain.Customer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class CustomerDao {

    public static Connection getConnection(){
        Connection con=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
        }catch(Exception e){System.out.println(e);}
        return con;
    }
 
    public static List<Customer> getRecords(int start,int size){
        List<Customer> list=new ArrayList<Customer>();

        try{
            Connection con=getConnection();
            PreparedStatement ps=con.prepareStatement("select * from customer limit "+start+","+size);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Customer customer=new Customer();
                customer.setId(rs.getInt("id"));
                customer.setName(rs.getString("name"));
                customer.setGender(rs.getString("gender"));
                customer.setTelephone(rs.getString("telephone"));
                customer.setAddress(rs.getString("address"));
                list.add(customer);
            }
        }catch(Exception e){
            System.out.println(e);
        }
        return list;
    }
}

5 编写view.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,com.yiidian.dao.*,com.yiidian.domain.*" %>
<html>
<head>
    <title>一点教程网-使用JSP完成分页</title>
</head>
<body>

<%

    String pageStr=request.getParameter("page");

    int curPage = 1;//当前页码
    int size=3;//每页显示记录数

    if(pageStr!=null && !pageStr.equals("")){
        curPage = Integer.parseInt(pageStr);
    }

    //计算每页起始行
    int start = (curPage-1)*size;

    List<Customer> list=CustomerDao.getRecords(start,size);

    out.print("<h1>当前页: "+curPage+"</h1>");
    out.print("<table border='1' cellpadding='4' width='60%'>");
    out.print("<tr><th>编号</th><th>名称</th><th>性别</th><th>联系电话</th><th>住址</th>");
    for(Customer e:list){
        out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td>"+
               " <td>"+e.getGender()+"</td><td>"+e.getTelephone()+"</td><td>"+e.getAddress()+"</td></tr>");
    }
    out.print("</table>");
%>
<a href="view.jsp?page=1">1</a>
<a href="view.jsp?page=2">2</a>
<a href="view.jsp?page=3">3</a>

</body>
</html>

6 运行测试

第一页访问地址:http://localhost:8080/view.jsp?page=1

 

热门文章

优秀文章