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

思想:分页需要四个变量:
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 getDatatList(int page){
    conn=fzConn.getConnection();
    ArrayList list = new ArrayList();
    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 list = db.getDatatList(currPage);

构建分页条:

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

将list传入至See.jsp页面:

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

See.jsp:


    
    
            
devid mobilevender appver osver origin eventid
${data.devid } ${data.mobilevender } ${data.appver } ${data.osver } ${data.origin } ${data.eventid}


<%=request.getAttribute("bar")%>

你可能感兴趣的:(mysql数据分页显示在jsp页面上)