servlet分页代码示例(2)

/**
     * 返回总页数
     * @return
     */
    public int getMaxPage(int rowsPerPage) {
        int maxPage;
        int maxRowCount = 0;
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            conn = DriverManager.getConnection(url,"scott","tiger");  //创建数据库连接
            String sql = "select count(*) from emp";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            if (rs.next()){
                maxRowCount = rs.getInt(1);  //总行数
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        maxPage = (maxRowCount + rowsPerPage - 1) / rowsPerPage;  //总页数
        return maxPage;
    }
}

3.创建 Servlet 对显示页面进行控制

复制代码 代码如下:


package com.servlet;

import com.dao.DBHelper;
import com.tool.UserData;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfdgz.html