mysql 分页类 PageBreaker

一个简洁的针对mysql数据库的分页类:

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.prj.db.DBManager;

public class PageBreaker {
	int intOffset;

	int intTotal;

	int intMaxLine;

	List listResults;

	int intTPages;

	int intCPages;

	String strPageQuery;

	String strQuery;

	String strQueryPart;

	String strFilePath;

	public PageBreaker() {
		intMaxLine = 10;
	}

	public List pageBreakQuery(String sql, HttpServletRequest req) {
		strFilePath = req.getRequestURI();
		int intBeginofsql = sql.indexOf("FROM");
		String strSQLAfterFrom = sql.substring(intBeginofsql, sql.length()).trim();
		strQuery = sql;
		strQueryPart = strSQLAfterFrom;
		String sqlCount = "select count(0) total "+strQueryPart;
		DBManager.log4gm.info("sqlCount: "+sqlCount);
		List listCount = DBManager.queryDB(sqlCount);
		
		for (int i = 0; i < listCount.size(); i++) {
			Map map = (Map)listCount.get(i);
			try {
				intTotal = Integer.parseInt(map.get("total").toString());
			} catch (Exception ex) {
				intTotal = 0;
			}
		}

		String strOffsetFromPage = req.getParameter("offset");
		if (strOffsetFromPage == null)
			intOffset = 0;
		else
			try {
				intOffset = Integer.parseInt(strOffsetFromPage);
			} catch (Exception ex) {
				intOffset = 0;
			}
		intTPages = (int) Math.ceil((double) intTotal / (double) intMaxLine);
		setIntTPages(intTPages);
		intCPages = (int) Math.floor((double) intOffset / (double) intMaxLine + 1.0D);
		setIntCPages(intCPages);
		if (intTotal > 0) {
			sqlCount = (new StringBuffer(strQuery)).append(
					" LIMIT ").append(intOffset).append(" , ").append(intMaxLine).toString();
			DBManager.log4gm.info((new StringBuilder("pageable sql: ")).append(sqlCount).toString());
			listResults = DBManager.queryDB(sqlCount);
		}
		return listResults;
	}
	
	public int getIntArea(String strCpage,int intAll) {
		int intOffIndex = 0;
		int intOff= 0;
		try {
			intOff = Integer.parseInt(strCpage);
		}catch (Exception e) {
		}
		if(intOff>0 && intOff<=intAll) {
			intOffIndex = intMaxLine *(intOff-1);
		}
		return intOffIndex;
	}

	public int getIntCPages() {
		return intCPages;
	}

	public int getIntTPages() {
		return intTPages;
	}
	
	

	public void setIntCPages(int intCPages) {
		this.intCPages = intCPages;
	}

	public void setIntTPages(int intTPages) {
		this.intTPages = intTPages;
	}

	public String PageLegend(HttpServletRequest req,String pagesTotal) {
		String str = "<form name=allpage action=\""+req.getContextPath()+"/product/productsave.jsp\" method=\"POST\" >"
			+"<input type=\"hidden\" value=\"pageoff\" name=strstatus2><input type=\"hidden\" value=\""+pagesTotal+"\" name=pages>" +
					"<li style=\"float:center; padding-left:8px; margin-top:-7px;\">";
		int first = 0;
		int next = intOffset + intMaxLine;
		int prev = intOffset - intMaxLine;
		int last = (intTPages - 1) * intMaxLine;
		if (intOffset >= intMaxLine)
			str = (new StringBuffer(str))
					.append("<A href=")
					.append(strFilePath).append("?offset=").append(first)
					.append(">首页</A> ").toString();
		else
			str = (new StringBuffer(str)).append(" 首页 ")
					.toString();
		if (prev >= 0)
			str = (new StringBuffer(str)).append(" <A href=")
					.append(strFilePath).append("?offset=").append(prev)
					.append(">前页</A> ").toString();
		else
			str = (new StringBuffer(str)).append(" 前页 ")
					.toString();
		if (next < intTotal)
			str = (new StringBuffer(str)).append(" <A href=")
					.append(strFilePath).append("?offset=").append(next)
					.append(">后页</A> ").toString();
		else
			str = (new StringBuffer(str)).append(" 后页 ")
					.toString();
		if (intTPages != 0 && intCPages < intTPages)
			str = (new StringBuffer(str)).append(" <A href=")
					.append(strFilePath).append("?offset=").append(last)
					.append(">尾页</A>").toString();
		else
			str = (new StringBuffer(str)).append(" 尾页 ")
					.toString();
			
		str = (new StringBuffer(str)).append(" 页次:").append(
				getIntCPages()).append("/").append(getIntTPages()).append("页 ")
				.toString();
		str = (new StringBuffer(str)).append(intMaxLine)
				.append("条/页 ").append("共").append(intTotal).append("条</li>")
				.toString();
		str = (new StringBuffer(str))
		.append(" 到第<input style=\"width:60px; height:20px; border:1px #dbdbdb solid\" name=\"whichone\" type=\"text\" />页,")
//		.append("<script language=\"javascript\">function getSelected(option){return(option);}")
		.append("<input style=\"width:60px; height:20px; border:1px #dbdbdb solid\"  type=\"submit\" value=\"跳转\"/></form>")
//		.append(" onclick=\"javascript:alert(prodelete.whichone.value)\"/>")
//		.append(strFilePath).append("?offset=prodelete.whichone.value'\"/>")
		.toString();
		return str;
	}
}

在查询时调用即可:
StringBuffer strbf = getProductSQL(strLimitation);
List list = pk.pageBreakQuery(strbf.toString().trim(), req);
================

你可能感兴趣的:(JavaScript,sql,mysql,jsp,servlet)