网页分页技术 案例分析

1.主页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
 <!-- 导入jstl文件 -->   
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<!-- 获取上下文环境 -->
<%
    String path = request.getContextPath();
    
%>

<!-- 导入jquery包 -->
<script src="../js/jquery-1.6.1.min.js"></script>
<!-- 绑定事件 -->
<script>
    $(function(){
        $(":button").bind("click",function(){
             location.href="<%=path%>/Myser";
        });
    });
</script>

<body>
<div style="margin-left:400px;">
<!-- 绑定单击事件 -->
<input type="button"  value="查询"/><br/>
    <table border="1">
        <tr>
            <th>序号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>性别</th>
            <th>生日</th>            
        </tr>
<!-- 用jstl表达式 循环遍历对象属性 -->
        <c:forEach items="${list }" var="s" varStatus="i"> 
        <tr>
            <td>${i.count }</td>
            <td>${s.getUserName() }</td>
            <td>${s.getPassword() }</td>
            <td>${s.getSex() }</td>
            <td>${s.getDate() }</td>            
        </tr>
        </c:forEach>
<!-- 底栏分页处理 -->        
        <tr>
            <td colspan="5" align="right">
                第${page.currentPage}页/共${page.totalPage }页 &nbsp;&nbsp;&nbsp;&nbsp;
                <c:if test="${page.currentPage>1}">
                    <a href="<%=path %>/Myser?currentP=${page.currentPage-1}">上一页</a>
                </c:if>    
                &nbsp;
                <c:if test="${page.currentPage < page.totalPage }">
                    <a href="<%=path %>/Myser?currentP=${page.currentPage+1}">下一页</a>
                </c:if>    
            </td>
        </tr>
    </table>
    
    </div>
</body>
</html>
mian.jsp

 

2,数据库连接--增删改与查询

package com.mian;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtil
{
    //锟斤拷锟捷匡拷
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/zhuece";
    private static String user="root";
    private static String password="123456";
    
    static Connection conn;
    static ResultSet set;
    static PreparedStatement ps;
    
    //锟斤拷锟斤拷锟斤拷锟斤拷
    static {
        try
        {
            Class.forName(driver);
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    //锟斤拷锟斤拷锟斤拷
    public static void openC()
    {
        try
        {
            conn=DriverManager.getConnection(url, user, password);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }        
    }
    //锟斤拷删锟侥的凤拷锟斤拷
    public static void up(String sql)
    {
        openC();
        try
        {
            ps=conn.prepareStatement(sql);
            ps.executeUpdate();
            
        } catch (SQLException e)
        {
            close();
            e.printStackTrace();
        }                
    }
    //锟斤拷询锟侥凤拷锟斤拷
    public static ResultSet cha(String sql,Object...args)
    {
        openC();
        try
        {
            ps=conn.prepareStatement(sql);
            if(args!=null)
            {
                int len=args.length;
                for(int i=0;i<len;i++)
                {
                    ps.setObject(i+1,args[i]);
                }
            }
            set=ps.executeQuery();
        } catch (SQLException e)
        {
            e.printStackTrace();
            close();
            return null;
        }        
        return set;  
    }
    
    public static void close()
    {
        if(set!=null)
        {
            try
            {
                set.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }finally
            {
                if(ps!=null)
                {
                    try
                    {
                        ps.close();
                    } catch (SQLException e)
                    {
                        e.printStackTrace();
                    }finally
                    {
                        if(conn!=null)
                        {
                            try
                            {
                                conn.close();
                            } catch (SQLException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
    
    
}
JDBCUtil.java


3.数据库中存的对象

package com.mian;

public class User
{
    //属性私有化
    private String userName;
    private String password;
    private String sex;
    private String date;
    private int id;
    
    //空参构造器
    public User()
    {        
    }
    //有参(所有属性)构造器
    public User(String userName, String password, String sex, String date ,int id)
    {
        super();
        this.userName = userName;
        this.password = password;
        this.sex = sex;
        this.date = date;
        this.id=id;
        
    }
    
    //提供公开的set和get方法
    public String getUserName()
    {
        return userName;
    }
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getSex()
    {
        return sex;
    }
    public void setSex(String sex)
    {
        this.sex = sex;
    }
    public String getDate()
    {
        return date;
    }
    public void setDate(String date)
    {
        this.date = date;
    }
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    
    
        
}
User.java

4.业务逻辑层,封装的增删改与查询的方法

package com.mian;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Page.Page;

public class LianJie
{

    public static List<User> find(Page page)
    {
        int current =page.getCurrentPage();
        int pageSize=page.getPageSize();
        
        String sql="select *from zhuce limit "+(current-1)*pageSize+","+pageSize;    
        
        ResultSet set=JDBCUtil.cha(sql);
        
        List<User> list = new ArrayList<User>();
        
        try
        {
            while(set.next())
            {
                String userNmae=set.getString("username");
                String password =set.getString("mima");
                String sex=set.getString("sex");
                String birth = set .getString("birthday");
                int id=set.getInt("id");
                
                User user = new User(userNmae,password,sex,birth,id);
                list.add(user);
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
            JDBCUtil.close();
        }
        JDBCUtil.close();
        return list;
    }
    
    public static int getTotal()
    {
        int count =0;
        String sql="select COUNT(1) from zhuce";
        ResultSet set=JDBCUtil.cha(sql);
        try
        {
            while(set.next())
            {
                count=set.getInt("COUNT(1)");
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
            JDBCUtil.close();
        }
        JDBCUtil.close();
        return count;
    }
}
LianJie.java

5.分页类,仅用于存储页数的数据

package Page;

public class Page
{
        //当前页
        private int currentPage;
        //总页数
        private int totalPage;
        //总记录数
        private int totalSize;
        //每页显示的记录数
        private int pageSize;
        
        public int getCurrentPage() {
            return currentPage;
        }
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
        public int getTotalPage() {
            totalPage = (int)Math.ceil(totalSize/(double)pageSize);
            return totalPage;
        }
        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }
        public int getTotalSize() {
            return totalSize;
        }
        public void setTotalSize(int totalSize) {
            this.totalSize = totalSize;
        }
        public int getPageSize() {
            return pageSize;
        }
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
}
Page.java

6.处理html的服务器

package myser;

import java.io.IOException;
import java.util.List;

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

import org.apache.commons.lang3.StringUtils;

import com.mian.LianJie;
import com.mian.User;

import Page.Page;

/**
 * Servlet implementation class Myser
 */
@WebServlet("/Myser")
public class Myser extends HttpServlet {
    private static final long serialVersionUID = 1L;
    int currentPage=1;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Myser() {
     
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize=10;
        Page page = new Page();
        int total=LianJie.getTotal();
        page.setTotalSize(total);
        page.setPageSize(pageSize);
        String currentP=request.getParameter("currentP");
        if(StringUtils.isNotBlank(currentP))
        {
            currentPage=Integer.valueOf(currentP);
        }
        page.setCurrentPage(currentPage);
        
        List<User> list=LianJie.find(page);
        HttpSession session = request.getSession();
        session.setAttribute("page", page);
        session.setAttribute("list", list);
        
        request.getRequestDispatcher("/fenye/mian.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}
Myser.java

 

posted @ 2017-08-02 18:46  千年虫虫  阅读(312)  评论(0编辑  收藏  举报