美文网首页
mysql数据分页显示在jsp页面上

mysql数据分页显示在jsp页面上

作者: 小小白园 | 来源:发表于2017-10-11 20:21 被阅读0次

    思想:分页需要四个变量:
    currPage(当前页码),pages(总页码),
    PAGE_SIZE(每个页面要显示的数据行数),count(表数据的总行数)

    部分代码如下:

    得到count:

    public int findCount(){
        int count=0;
        String sql = "select count(*) from appinfo";
        try{
            Statement sta = conn.createStatement();
            ResultSet rs = sta.executeQuery(sql);
            if(rs.next()){
                count = rs.getInt(1);  //对总记录数赋值
            }
            rs.close();
            conn.close();
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;        //返回总记录数
    } 
    

    得到pages:

    if(count%Data.PAGE_SIZE==0){
        pages=count/Data.PAGE_SIZE;
    }else{
        pages=count/Data.PAGE_SIZE+1;
    }
    

    得到currPage:

    if(request.getParameter("page")!=null){
        currPage=Integer.parseInt(request.getParameter("page"));
    }
    

    每一个分页要显示的数据list:

    //getDatatList方法根据入口参数page传递的页码,查询指定页码中的数据
    
    public ArrayList<Data> getDatatList(int page){
        conn=fzConn.getConnection();
        ArrayList<Data> list = new ArrayList<Data>();
        String sql = "select* from appinfo limit ?,?";
            
        // 取得数据库操作对象
        try{
            PreparedStatement  ps= conn.prepareStatement(sql);
            ps.setInt(1, (page-1)*Data.PAGE_SIZE);
            ps.setInt(2, Data.PAGE_SIZE);
            ResultSet rs = ps.executeQuery();
                
            // 循环记录集,查看每一行每一列的记录
            while (rs.next()) {
                String sdevid=rs.getString(1);
                String smobilevender=rs.getString(2);
                String sappver=rs.getString(3);
                String sorigin=rs.getString(5);
                String seventid=rs.getString(6);
                Data data = new Data();
                data.setDevid(sdevid);
                data.setMobilevender(smobilevender);
                data.setAppver(sappver);
                data.setOsver(sosver);
                data.setOrigin(sorigin);
                data.setEventid(seventid);
                list.add(data);
            }
            ps.close();   
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            return list;
    }
    

    调用getDatatList方法,传入实参currPage,得到当前页面显示的数据list:

    SeeOMethod db = new SeeOMethod();
    ArrayList<Data> list = db.getDatatList(currPage);
    

    构建分页条:

    StringBuffer sb = new StringBuffer();
    //通过循环构建分页条
    for(int i=1;i<=pages;i++){
        if(i==currPage){   //判断是否为当前页
            sb.append("『"+i+"』");  //构建分页条
        }else{
            sb.append("<a href='SeeDataServlet?page="+i+"'>"+i+"</a>");
        }
        sb.append(" ");
    }
    request.setAttribute("bar", sb.toString());
    

    将list传入至See.jsp页面:

    request.setAttribute("list", list);
    request.getRequestDispatcher("See.jsp").forward(request, response);
    

    See.jsp:

    <body>
        
        <table align="center"  border="1">
            <tr>
                <td>devid</td>
                <td>mobilevender</td>
                <td>appver</td>
                <td>osver</td> 
                <td>origin</td>
                <td>eventid</td>   
            </tr>
            <c:forEach items="${list}" var="data">
                <tr>
                    <td>${data.devid }</td>
                    <td>${data.mobilevender }</td> 
                    <td>${data.appver }</td>
                    <td>${data.osver }</td> 
                    <td>${data.origin }</td>
                    <td>${data.eventid}</td> 
                </tr>
            </c:forEach>    
        </table>
    
        <br></br>
    
        <table border="1">
            <tr>
                <td align="center" colspan="10">
                   <%=request.getAttribute("bar")%>
                </td>
            </tr>
        </table>
    </body>
    

    相关文章

      网友评论

          本文标题:mysql数据分页显示在jsp页面上

          本文链接:https://www.haomeiwen.com/subject/fkfgyxtx.html