- import java.sql.*;
- import javax.servlet.http.*;
- import database.GetConnection;
- public class PageQuery {
-
- int offset; // 记录偏移量
- int total; // 记录总数
-
- int maxline; // 记录每页显示记录数
- ResultSet rs; // 读出的结果
- int tpages; // 总页数
- int cpages; // 当前页数
- String pagequery; // 分页显示要传递的参数
- String query; // query 语句
- String querypart; // " from " 以后的 query 部分
- String str= "";
- String str1= "";
- String filepath;
-
- Connection conn; // object of dbclass
-
- //constructer do nothing
- public PageQuery() throws Exception{
- // 每页显示十行
- maxline = 10;
- conn=(Connection)new GetConnection().getConnection();
- }
-
- //********读取记录***************
- // 主要工作函数,根据所给的条件从表中读取相应的记录
- public ResultSet myquery(String category,String name,HttpServletRequest req) throws SQLException {
-
- String query_part, os;
- int begin;
- String query = "select * from " + name + " where category='" + category + "' order by id desc";
- // 截取 " from " 以后的 query 语句
- begin = query.indexOf("from");
- query_part = query.substring(begin, query.length()).trim();
-
- // 计算偏移量
- os = req.getParameter("offset");
- if (os == null) offset = 0;
- else offset = Integer.parseInt(os);
-
- // 获取文件名
- filepath = req.getRequestURI();
- querypart = query_part;
-
- // 计算总的记录条数
- String sql = "select count(*) as total " + this.querypart;
- PreparedStatement psmt=conn.prepareStatement(sql);
-
- rs = psmt.executeQuery(sql);
- if (rs.next())
- total = rs.getInt(1);
- // 设置当前页数和总页数
- tpages = (int)Math.ceil((double)this.total/this.maxline);
- cpages = (int)Math.floor((double)offset/this.maxline+1);
- // 根据条件判断,取出所需记录
- if (total > 0) {
- sql = query + " limit " + offset + " , " + maxline;
- psmt=conn.prepareStatement(sql);
- rs = psmt.executeQuery(sql);
- }
- return rs;
- }
- // 显示总页数
- public int gettotalpages() {
- return tpages;
- }
- //显示当前所在页数
- public int getcurrenpages() {
- return cpages;
- }
- public ResultSet myqueryEM(HttpServletRequest req) throws SQLException {
-
- String query_part, os;
- int begin;
- String query = "select * from employee order by id desc";
- // 截取 " from " 以后的 query 语句
- begin = query.indexOf("from");
- query_part = query.substring(begin, query.length()).trim();
-
- // 计算偏移量
- os = req.getParameter("offset");
- if (os == null) offset = 0;
- else offset = Integer.parseInt(os);
-
- // 获取文件名
- filepath = req.getRequestURI();
- querypart = query_part;
-
- // 计算总的记录条数
- String sql = "select count(*) as total " + this.querypart;
- PreparedStatement psmt=conn.prepareStatement(sql);
-
- rs = psmt.executeQuery(sql);
- if (rs.next())
- total = rs.getInt(1);
- // 设置当前页数和总页数
- tpages = (int)Math.ceil((double)this.total/this.maxline);
- cpages = (int)Math.floor((double)offset/this.maxline+1);
- // 根据条件判断,取出所需记录
- if (total > 0) {
- sql = query + " limit " + offset + " , " + maxline;
- psmt=conn.prepareStatement(sql);
- rs = psmt.executeQuery(sql);
- }
- return rs;
- }
- //**********显示翻页提示栏*************
- // 显示首页、下页、上页、尾页
- // 你可以改成你喜欢的样式
- public String pagelegend() {
- int first, next, prev, last;
- first = 0;
- next = offset + maxline;
-
-
- prev = offset - maxline;
- last = (this.tpages - 1) * maxline;
-
- if(offset >= maxline)
- str += " + filepath + "?offset=" + first + ">首页 ";
- else str += " 首页 ";
- if(prev >= 0)
- str += " + filepath + "?offset=" + prev + ">前页 ";
- else str += " 前页 ";
- if(next < total)
- str += " + filepath + "?offset=" + next + ">后页 ";
- else str += " 后页 ";
- if(tpages != 0 && cpages < tpages)
- str += " + filepath + "?offset=" + last + ">尾页";
- else str += " 尾页 ";
- str += " 页次:" + getcurrenpages() + "/" + gettotalpages() + "页 ";
- str += maxline + "条/页 " + "共" + total + "条";
- return str;
- }
- }