2021-04-21 22:06  阅读(119)
文章分类:JSP 简单教程 文章标签:JSP
©  原文作者:一点教程 原文地址:http://www.yiidian.com/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)插入一些测试数据,如下:

202104212201112_1.png

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("| 编号|名称|性别|联系电话|住址|
| :-----: | :-----: | :-----: | :-----: | :-----: | 
| "+e.getId()+" | "+e.getName()+" | "+e.getGender()+" | "+e.getTelephone()+" | "+e.getAddress()+" | 
");
    %>
    <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

2021042122018_2.png

2021042122011_3.png202104212209_4.png

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> 使用JSP完成分页
上一篇
使用JSP完成CRUD
下一篇
使用JSP完成用户注册