码迷,mamicode.com
首页 > Web开发 > 详细

jsp分页

时间:2017-06-16 22:06:19      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:etc   idt   ceil   art   ott   显示   nbsp   信息   border   

<%

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>

jsp分页

标签:etc   idt   ceil   art   ott   显示   nbsp   信息   border   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!