package com.wen;
/**
* 分页组件
* @author explorer
*/
import java.util.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.wen.Linkdb;
public class Pagi {
ResultSet CountTopicrs = null; //初始化总记录数Rs变量
ResultSet Pagirs = null; //初始化分页时每页的记录集数Rs变量
private int intCountTopic = 0; //主题总数,即select选出的、库中所有记录总数
public int intPageSize; //每页显示主题数,即每页显示的记录总数
public int intPageCount; //总页数
public int intPage = 0; //当前页数
private String Countsql = null, Pagisql = null, str = null, str_where = null;
private String str_parameter = "";
//public static int pages_n=1; //传分页参数值
private String nowPage; //初始化当前页intPage变量,以准确便获取当前页,即获取当前页的具体页号。
private String HttpFile; //当前的地址栏的文件,即具体jsp文件。
Linkdb db = new Linkdb(); //连接数据库
//接收传分页参数
public void setPages(int n) {
intPageSize = n;
}
/*功能:接收参数组织SQL语句
*str_table :分页显示的表名
*str_where:分页的where条件
*httpfile :具体jsp文件
*pages :获取地址栏传过来的pages参数
*/
public ResultSet setQuerysql(String str_table, String str_where,
String httpfile, String pages) throws
SQLException {
ResultSet r = null;
this.nowPage = pages;
this.HttpFile = httpfile; //分页文件名
Countsql = "select count(*) from " + str_table + " " + str_where;
Pagisql = "select * from " + str_table + " " + str_where;
try {
r = querySql(Countsql, Pagisql);
}
catch (SQLException _ex) {
System.out.println(_ex);
}
return r;
}
/*功能:接收参数进行首尾页判断
*Countsql:总记录的Query字符串。[形式为select count(*) from tablename]
*Pagisql :要分页的Query字符串。[形式为select * from tablename where ...]
*request :参数传递过程中的变量。[用来控制翻页时的pages变量]
*/
public ResultSet querySql(String Countsql, String Pagisql) throws //,HttpServletRequest request
SQLException {
//获取当前文件名。
//HttpFile=request.getRequestURI();
//获取当前页,将数值赋予intPage变量。[分页栏中必须要有pages参数]
//nowPage=request.getParameter("pages");//由参数HttpServletRequest request传递而来
if (nowPage == null) {
intPage = 1;
}
else {
intPage = Integer.parseInt(nowPage);
if (intPage < 1) {
intPage = 1;
}
} //end else
//获取总记录数的结果集。
CountTopicrs = db.executeQuery(Countsql);
if (CountTopicrs.next()) {
intCountTopic = CountTopicrs.getInt(1); //获取第一个字段的整型
}
//获取总页数。
intPageCount = (intCountTopic + intPageSize - 1) / intPageSize;
//如果当前页大于总页数,则当前页等于总页数。//=最后一页
if (intPage > intPageCount) {
intPage = intPageCount;
}
//关闭总主题数的数据集。
CountTopicrs.close();
//获取执行分页的结果集。
Pagirs = db.executeQuery(Pagisql);
return Pagirs;
} //end querySql function.
//获取记录总数。
public int getCountTopic() {
return intCountTopic;
}
//获取总页数。
public int getPageCount() {
return intPageCount;
}
//获取当前页数。
public int getIntPage() {
return intPage;
}
//获取当前页的数据。boodata为True,表示要加入该数据到当前页。
//这里可能会在JSP调用时影响速度[因为调用时要多一层循环],因此放到JSP中嵌入,待改进。
//该代码暂时保留。
// public boolean getData(){
// boolean boodata=false;
// if (intPageCount>0)
// {
// try
// {
// while (Pagirs.next())
// {
// i++;
/// if (i>((intPage-1)*intPageSize) &&(i<=intPage*intPageSize))
// {
// boodata=true;
// }
// } //endwhile.
// }//end try.
// catch(Exception e){
// System.out.println(e.toString());
// }
// } //endif.
// return boodata;
// } //end getData();
//设置分页参数
public void setPfoot(String str) {
this.str_parameter += str;
}
//分页栏函数。
public String PageFooter() {
String str = "";
int next, prev;
prev = intPage - 1;
next = intPage + 1;
str += "<font style='font-size: 9pt'>总计<font color='red'>" + getCountTopic() +
"</font>条记录," + "【共<font color='red'>" + getPageCount() + "</font>页】";
str += "【条" + intPageSize + "/页】 当前第<font color='red'>" + getIntPage() +
"</font>页(列出第" + ( (intPageSize * getIntPage() + 1) - intPageSize) +
"到第" + (getIntPage() * intPageSize) + "条) "; //getIntPage()*intPageSize
if (intPage > 1) {
str += " <A href=" + HttpFile + "?pages=1" + str_parameter + ">第一页</A> ";
}
else {
str += " 第一页 ";
}
if (intPage > 1) {
str += " <A href=" + HttpFile + "?pages=" + prev + str_parameter +
">上一页</A> ";
}
else {
str += " 上一页 ";
}
if (intPage < intPageCount) {
str += " <A href=" + HttpFile + "?pages=" + next + str_parameter +
">下一页</A> ";
}
else {
str += " 下一页 ";
}
if (intPageCount > 1 && intPage != intPageCount) {
str += " <A href=" + HttpFile + "?pages=" + intPageCount + str_parameter +
">最后页</A>";
}
else {
str += " 最后页 </font>";
}
str += " 转到<INPUT TYPE='text'NAME='go' size='2'>页 <input type='submit' name='Submit' value='go'>";
return str;
}
//关闭数据库连接
public void closeConn() {
db.closeStmt();
db.closeConn();
}
}