自己写的分页组件

1.封装类代码如下:

import java.sql.SQLException;
import java.util.List;

import javax.swing.text.TableView.TableRow;

@SuppressWarnings("unchecked")
public class PaginationModule {
	
	private int NumPerPage = 15;													//每页显示的记录条数
	private int currentPage = 0;												   //当前显示页面
	private int totalNums = 0;													  //共有多少条数据
	private int totalPages = 0;													 //可以分成多少个页面
	private int cfPage = 0;													    //要显示页面的第一条数据
	private int clPage = 0;												       //要显示页面的最后一条数据
	private int firstOfPage = 0;									          //显示第一页面
	private int prevOfPage = 0;							        		     //显示最后一页面
	private int lastOfPage = 0;									            //显示前一页面
	private int nextOfPage = 0;									           //显示下一页面
	private String keyword = null;									      //关键字查询
	private String tabelName = "";								         //访问数据库中的一个表或子查询
	private String condition = "";								        //条件查询	
	private String orderby = "";								       //按某字段排序返回结果
	private boolean desc = false;							          //是否按逆序进行排序
	private String url = "";								         //页面的请求路径
	private boolean flag = false;								    //是否在数据前面显示4个按钮
	
	private List list = null;								            //数据存储于list中
	private TableRowIterator tr = null;						            //返回一个TableRowIterator
	private Context context = null;
	
	
	
	public PaginationModule(Context context) {
		this.context = context;
	}
	
	public void setNumPerPage(int numPerPage) {
		NumPerPage = numPerPage;
	}
	
	public void setTabelName(String tabelName) throws SQLException {
		this.tabelName = tabelName;
		if(!"".equals(this.tabelName)){
			this.initTotalNum();
		}
	}	
	
	public void setTabelName(String tabelName, String condition)throws SQLException  {
		this.tabelName = tabelName;
		//condition = condition.trim().replaceAll("'", "");
		String temp = condition;
		String tp = "";
		int ii = temp.indexOf("%");
		if(ii != -1) {
			do {
				int i = temp.indexOf("%");
				String sub1 = temp.substring(0, i+1);
				String sub2 = temp.substring(i+1, temp.length());
				int j = sub2.indexOf("%");
				tp += sub1;
				if(j != 0) {
					String ktemp = sub2.substring(0, j).trim().replaceAll("'", "");
					tp += ktemp;
				}
				tp += "%";
				temp = sub2.substring(j+1, sub2.length());
			} while(temp.indexOf("%") > 0);
		}
		temp = tp + temp;
		this.condition = temp;
		if(!"".equals(this.condition)){
			this.initTotalNum(this.condition);
		} else if(!"".equals(this.tabelName)){
			this.initTotalNum();
		}
	}
	
	public void setCurrentPage(int currentPage) throws SQLException  {
		
		if(currentPage >= this.totalPages) {
			currentPage = this.totalPages - 1;
		}
		if(currentPage < 0) {
			currentPage = 0;
		}
		this.cfPage = currentPage*NumPerPage+1;
		this.clPage = currentPage*NumPerPage+NumPerPage > totalNums ? totalNums:currentPage*NumPerPage+NumPerPage;
		this.firstOfPage = currentPage == 0 ? -1 : 0;
		this.prevOfPage = currentPage == 0 ? -1 : currentPage-1;
		this.lastOfPage = totalPages == 1 ? -1 : totalPages-1;
		this.nextOfPage = totalPages > 1 ? currentPage + 1 : -1;
		this.currentPage = currentPage;
		
		if(!"".equals(this.condition)){
			this.initdata(this.condition);
		} else if(!"".equals(this.tabelName)){
			this.initdata();
		}
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public PaginationModule() {
		
	}
	
	public PaginationModule(int numPerPage, int currentPage, String table, String url, Context context) {
		this.NumPerPage = numPerPage;
		this.tabelName = table;
	}
	
	//推荐使用此构造方法
	public PaginationModule(int numPerPage, int currentPage, int totalNums, List list, String url, Context context) {
		this.NumPerPage = numPerPage;
		this.currentPage = currentPage;
		this.totalNums = totalNums;
		this.list = list;
		this.totalPages = (totalNums+NumPerPage-1)/NumPerPage;
		this.cfPage = currentPage*NumPerPage+1;
		this.clPage = currentPage*NumPerPage+NumPerPage > totalNums ? totalNums:currentPage*NumPerPage+NumPerPage;
		this.firstOfPage = currentPage == 0 ? -1 : 0;
		this.prevOfPage = currentPage == 0 ? -1 : currentPage-1;
		this.lastOfPage = totalPages == 1 ? -1 : totalPages-1;
		this.nextOfPage = totalPages > 1 ? currentPage + 1 : -1;
		this.url = url;
		this.context = context;
	}
	
	public int getNumPerPage() {
		return NumPerPage;
	}
	
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public void initdata() throws SQLException{
		Object[] parameters = new Integer[2];
		parameters[0] = this.cfPage;
		parameters[1] = this.clPage;
		String sql = "select rn, ne.* from (select rownum rn,n.* from(select * from ";
		if(this.desc) {
			sql = sql + this.tabelName + " order by " + this.orderby +" desc ) n)  ne where rn between ? and ?";
		} else if(!"".equals(this.orderby)) {
			sql = sql + this.tabelName + " order by " + this.orderby +" ) n)  ne where rn between ? and ?";
		
		} else {
			sql = sql + this.tabelName + " ) n)  ne where rn between ? and ?";
		}
		this.tr = DatabaseManager.query(context, sql, parameters);
	}
	
	public void initdata(String condition) throws SQLException{
		Object[] parameters = new Integer[2];
		parameters[0] = this.cfPage;
		parameters[1] = this.clPage;
		String sql = "select rn, ne.* from (select rownum rn,n.* from(select * from ";
		if(this.desc) {
			sql = sql + this.tabelName + " where " + condition + " order by " + this.orderby + " desc ) n)  ne where rn between ? and ?";
		} else if(!"".equals(this.orderby)) {
			sql = sql + this.tabelName + " where " + condition +
			  " order by " + this.orderby + " ) n)  ne where rn between ? and ?";
		} else {
			 sql = sql + this.tabelName + " where " + condition + ") n)  ne where rn between ? and ?";
		}
		this.tr = DatabaseManager.query(context, sql, parameters);
			}
	
	public void initTotalNum() throws SQLException{
		String sql = "select count(*) num from " + this.tabelName;
		TableRowIterator tr = DatabaseManager.query(context, sql);
		if(tr.hasNext()) {
			this.totalNums = tr.next().getIntColumn("num"); 
		}
		this.totalPages = (totalNums+NumPerPage-1)/NumPerPage;
	}
	
	public void initTotalNum(String condition) throws SQLException{
		String sql = "select count(*) num from " + this.tabelName + " where " + condition;
		TableRowIterator tr = DatabaseManager.query(context, sql);
		if(tr.hasNext()) {
			this.totalNums = tr.next().getIntColumn("num"); 
		}
		this.totalPages = (totalNums+NumPerPage-1)/NumPerPage;
	}
	
	public TableRowIterator executeSql(Context context, String sql ) throws SQLException{
		return DatabaseManager.query(context, sql);
	}
	
	public int getCfPage() {
		return cfPage;
	}
	public void setCfPage(int cfPage) {
		this.cfPage = cfPage;
	}
	
	public int getClPage() {
		return clPage;
	}
	public void setClPage(int clPage) {
		this.clPage = clPage;
	}
	
	public int getTotalNums() {
		return totalNums;
	}

	public void setTotalNums(int totalNums) {
		this.totalNums = totalNums;
		this.totalPages = (totalNums+NumPerPage-1)/NumPerPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	public List getList() {
		return list;
	}
	
	public void setList(List list) {
		this.list = list;
	}
	
	public int getFirstOfPage() {
		return firstOfPage;
	}

	public void setFirstOfPage(int firstOfPage) {
		this.firstOfPage = firstOfPage;
	}

	public int getPrevOfPage() {
		return prevOfPage;
	}

	public void setPrevOfPage(int prevOfPage) {
		this.prevOfPage = prevOfPage;
	}

	public int getLastOfPage() {
		return lastOfPage;
	}

	public void setLastOfPage(int lastOfPage) {
		this.lastOfPage = lastOfPage;
	}

	public int getNextOfPage() {
		return nextOfPage;
	}
	
	public void setNextOfPage(int nextOfPage) {
		this.nextOfPage = nextOfPage;
	}
	public String getUrl() {
		return url;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
	public TableRowIterator getTr() {
		return tr;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public void setContext(Context context) {
		this.context = context;
	}
	public void setOrderby(String orderby, boolean desc) {
		this.orderby = orderby;
		this.desc = desc;
	}
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		keyword = keyword.trim().replaceAll("'", "");
		this.keyword = keyword;
	}
}

2.主要由一个类进行了封装,servlet进行相关参数的设置。Servlet代码如下:
PaginationModule pagm = new PaginationModule(c);
		int currentPage = 0;
		if (request.getParameter("curp") != null) {
			currentPage = Integer.parseInt(request.getParameter("curp"));
		}
		pagm.setOrderby("created_date", true);
		String keyword = request.getParameter("conditionvalue") == null ? "":request.getParameter("conditionvalue");
				
		pagm.setTabelName("news", "title like '%" + keyword + "%' ");
			
		pagm.setKeyword(keyword);
		try {
			if (request.getParameter("jump_page") != null) {
				currentPage = Integer.parseInt(request.getParameter("jump_page")) - 1;
			}
		} catch (NumberFormatException e) {
			currentPage = 0;
		}
		
		pagm.setCurrentPage(currentPage);
		TableRowIterator tr = pagm.getTr();
		if(tr != null) {
			List<News> newslist = new ArrayList<News>();
			while(tr.hasNext()) {
				TableRow trw = tr.next();
				if(trw != null) {
					newslist.add( new News(trw));
				}
			}
			pagm.setList(newslist);
		}
		pagm.setUrl("news-edit?conditionvalue=" + java.net.URLEncoder.encode(keyword, Constants.DEFAULT_ENCODING) + "&amp;");	
		request.setAttribute("paginationModule", pagm);
3.jsp页面显示:
<%
   
    PaginationModule pmodule = (PaginationModule)request.getAttribute("paginationModule");
    List<News> newslist = pmodule.getList();
    String keyword = pmodule.getKeyword();
%>
	         
<% 	if (pmodule.getFirstOfPage() > -1) { %>                                    
         &nbsp;<a href="<%=request.getContextPath() %>/<%=pmodule.getUrl() %>curp=0" ><img  border="0" src="<%= request.getContextPath() %>/images/btn_prev2-1.gif" width="12" height="9" alt=""/></a>
<%	} else {  %>                                    
         &nbsp;<img  border="0" src="<%= request.getContextPath() %>/images/btn_prev2.gif" width="12" height="9" alt=""/>
<%	} %>  
			
<%	if (pmodule.getCurrentPage() > 0) {  %>                                    
         &nbsp;<a href="<%=request.getContextPath() %>/<%=pmodule.getUrl() %>curp=<%=pmodule.getPrevOfPage() %>" ><img  border="0" src="<%= request.getContextPath() %>/images/btn_prev-1.gif" width="5" height="9" alt=""/></a>
<%	} else { %>                                    
         &nbsp;<img  border="0" src="<%= request.getContextPath() %>/images/btn_prev.gif" width="5" height="9" alt=""/>
<%	}  %>     
		<fmt:message key="jsp.general.showresults">
			<fmt:param value="<%= Integer.toString(pmodule.getCfPage()) %>"/>
			<fmt:param value="<%= Integer.toString(pmodule.getClPage()) %>"/>
			<fmt:param value="<%= Integer.toString(pmodule.getTotalNums()) %>"/>
			<fmt:param value="<%= Integer.toString(pmodule.getCurrentPage()+1) %>" />
			<fmt:param value="<%= Integer.toString(pmodule.getTotalPages()) %>" />
		</fmt:message>
<% if (pmodule.getNextOfPage()>-1 && pmodule.getCurrentPage() < pmodule.getTotalPages() -1) { %>                                    
         &nbsp;<a href="<%=request.getContextPath() %>/<%=pmodule.getUrl() %>curp=<%=pmodule.getNextOfPage() %>" ><img  border="0" src="<%= request.getContextPath() %>/images/btn_next-1.gif" width="5" height="9" alt=""/></a>
<%	} else { %>                                    
         &nbsp;<img  border="0" src="<%= request.getContextPath() %>/images/btn_next.gif" width="5" height="9" alt=""/>
<%	}  %>  
	<%-- Last Page --%> 
<%	if (pmodule.getLastOfPage()>-1 && pmodule.getCurrentPage() < pmodule.getTotalPages() -1) { %>                                    
         &nbsp;<a href="<%=request.getContextPath() %>/<%=pmodule.getUrl() %>curp=<%=pmodule.getLastOfPage() %>" ><img  border="0" src="<%= request.getContextPath() %>/images/btn_next2-1.gif" width="12" height="9" alt=""/></a>
<%	} else { %>                                    
         &nbsp;<img  border="0" src="<%= request.getContextPath() %>/images/btn_next2.gif" width="12" height="9" alt=""/>
<%	}  %>         
				</td><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;
	<input id="jump_page" name="jump_page" onkeypress="javascript:return disableEnterKey(event);" class="bd" size="3" maxlength="45" value=""/> 
	<input type="submit" class="btn" name="submit_search" value="<fmt:message key="jsp.page.goto"/>"/>
	</td></tr>
			</table></td></tr>
			</tbody>
		</table>
		
		</div> 
		</form>
</div>		<% } 


你可能感兴趣的:(sql,jsp,swing,servlet,J#)