jsp mysql分页显示_mysql在JSP页面中分页查看的解决

最近一直在做期末的项目,所以遇见了很多问题,解决了,就会在这里写出来和大家分享。。。。

用了一个上午,自己想,和参考一些东西以后,做出了数据库分页查看的功能,觉得还算清晰吧,希望大家多多指教。。。

思路是这样的:使用javabean来操作分页的控制数据,然后在jsp里实现,在页面最后的下一页之类的连接,增加了参数值的判断,完美的解决了一些问题。

详细的看源码吧。。。

View.java

public class View {

private int currentPage;// 记录当前的页数

private int pageSize;// 每页显示的记录数

private int recordCount;// 一共有多少条记录

public View(int pageSize, int recordCount, int currentPage) {

this.pageSize = pageSize;

this.recordCount = recordCount;

this.setCurrentPage(currentPage);

}

// 计算总的页数

public int getPageCount() {

int size = recordCount / pageSize;// 总页数=总条数/每页要现实的记录

int flag = recordCount % pageSize;// 取模运算,为了计算最后一页要显示的条数

if (flag != 0) {

size++;

}

// 根据记录数判断返回的总页数

if (recordCount == 0) {// 如果有0条记录

// 返回只有1页

return 1;

}

// 返回计算出来的页数

return size;

}

// 设置sql语句中,limit的索引起始位置,从0开始

public int getFromIndex() {

return (currentPage - 1) * pageSize;// 重点算法:(当前页码-1)*每页记录数=索引的起始位置

}

// 设置当前页

public void setCurrentPage(int currentPage) {

// 为什么要设置?

/*

* 因为:在jsp页面中,会放置首页和上一页,下一页和末页两个按钮

* 当在最后一页点击下一页时,此时获得页面数会大于实际的页面,所以页面就要保持在最后一页

* 当在第一页点击上一页,此时获得的页面数会小于或=0,此时就要一直保持在第一页

*/

int vaildPage = currentPage <= 0 ? 1 : currentPage;

vaildPage = vaildPage > this.getPageCount() ? this.getPageCount()

: vaildPage;

this.currentPage = vaildPage;

}

// 得到当前页,用于显示在JSP中

public int getCurrentPage() {

return currentPage;

}

// 得到每页显示的记录数

public int getPageSize() {

return pageSize;

}

}

index.jsp

分页查看数据库

public static final String URL = "jdbc:mysql://localhost:3306/j2ee";

public static final String USERNAME = "root";

public static final String PASSWORD = "root";%>

//连接数据库,获取有多少条记录

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

Connection conn = DriverManager.getConnection(URL, USERNAME,

PASSWORD);

String sqlCount = "select count(*) from employee";//得到一共有多少条记录

PreparedStatement ps = conn.prepareStatement(sqlCount);

ResultSet rs = ps.executeQuery();

int recordCount = 0;//一共有多少记录

if (rs.next()) {

recordCount = rs.getInt(1);

}

%>

//获取分页对象,传参

String pageStr = request.getParameter("page");

int currentPage = 1;

if (pageStr != null) {

currentPage = Integer.parseInt(pageStr);

}

View view = new View(5, recordCount, currentPage);

//获取参数

int fromIndex = view.getFromIndex();//起始位置

int pageSize = view.getPageSize();//每页显示的记录数

%>

//执行sql

String sql = "select fname,lname from employee limit ?,?";

ps = conn.prepareStatement(sql);

ps.setInt(1, fromIndex);

ps.setInt(2, pageSize);

rs = ps.executeQuery();

while (rs.next()) {

String fname = rs.getString(1);

String lname = rs.getString(2);

%>

}

%>

//关闭连接

if (ps != null) {

ps.close();

}

if (rs != null) {

rs.close();

}

if (conn != null) {

conn.close();

}

%>

当前第页,一共页

首页

if ((view.getCurrentPage() - 1) <= 0) {

%>

上一页

} else {

%>

上一页

}

%>

if ((view.getCurrentPage() + 1) > view.getPageCount()) {

%>

} else {

%>

下一页

}

%>

末页

摘自:技术豆子的博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值