jsp分页

<%

int pagesize = 3;//设置每页信息的个数

int totalrows = 0;//一共多少行

int totalpage = 0;//一共多少页

int curpage = 1;//当前页

 

String pageString = request.getParameter("curpage");//获取当前页

if (pageString != null && pageString.matches("\d+")) {//判断当前页是否为空是否为数字

curpage = Integer.parseInt(pageString);

}

 

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.2.63:3306/lianxi", "lianxi", "lianxi");

String sql = "select count(id) as totalrows from liuyan_list";

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql);

if (rs.next()) {

totalrows = rs.getInt(1);//因为受影响行数返回值就1行

totalpage = (int) Math.ceil(totalrows * 1.0 / pagesize);//总行数变成双精度向上取整

}

if (totalpage != 0 && curpage >= totalpage) {//当前页大于总页数

curpage = totalpage;

}

 

sql = "select * from liuyan_list_view order by id desc limit " + (curpage - 1) * pagesize + ",10";//显示第几条及1以后10条数据

rs = stmt.executeQuery(sql);//重新赋值

int floor = 0;//显示当前页面第几条

while (rs.next()) {

floor++;

%>

 

<div>

每页<% out.print(pagesize); %>条 当前第<% out.print(curpage); %>页 总<% out.print(totalpage); %>页

<%

int step = 5;

 

int start = (int) Math.ceil(curpage * 1.0 / step);

start = (start - 1) * step + 1;

int end = start + step;

if (totalpage < end) {

end = totalpage + 1;

}

if (curpage > 1) {

out.print("<a href='?curpage=1'>首页</a> <a href='?curpage=" + (curpage - 1) + "'>上一页</a>");

}

for (int i = start; i < end; i++) {

out.print("<a href='?curpage=" + i + "' " + (curpage == i ? "class='current'" : "") + ">" + i + "</a>");

}

if (curpage < totalpage) {

out.print("<a href='?curpage=" + (curpage + 1) + "'>下一页</a> <a href='?curpage=" + totalpage + "'>末页</a>");

}

%>

</div>

原文地址:https://www.cnblogs.com/bonly-ge/p/7029258.html