分页工具类

 

工具类:

package com.jungle.utils; import java.sql.CallableStatement; import java.sql.Connection; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import oracle.jdbc.driver.OracleTypes; public class MyPageTool { private int currentpage = 1;// 当前页 private int pageSize = 3;// 每页显示多少条 private int rowCount; // 共有多少条 private int pageCount;// 共有多少页 private HttpServletRequest request; private Connection connection; /** * 得到分页的字符串! * * @param request * request请求 * @param connection * 数据库连接 * @param sql * 查询sql * @param currentPage * 当前页 * @param pageSize * 每页显 示条数 * @throws Exception */ public MyPageTool(HttpServletRequest request, Connection connection, String sql, int currentPage, int pageSize) throws Exception { this.currentpage = currentPage; this.pageSize = pageSize; this.request = request; this.connection = connection; CallableStatement cstmt = connection .prepareCall("{call package_page.proc_page(?,?,?,?,?)}"); cstmt.setString(1, sql); cstmt.setInt(2, pageSize); cstmt.setInt(3, currentPage); cstmt.registerOutParameter(4, OracleTypes.INTEGER); cstmt.registerOutParameter(5, OracleTypes.INTEGER); cstmt.execute(); rowCount = cstmt.getInt(4); pageCount = cstmt.getInt(5); } /** * 得到分页字符串! 下一步: <br> * sql += " and rn>" + pagesize * (currentpage - 1) + <br> * " and rn<=" + pagesiz * currentpage;<br> * */ public String getPageString() { return getsimplePage(); } /** * 存储过程代码:<br> * create or replace package package_page as <br> * Procedure proc_page(p_sql varchar2, --查询的sql <br> * p_pagesize Number, --每页大小 <br> * p_curpage Number, --当前页 <br> * p_rowcount out Number, --总条数,输出参数 <br> * p_pagecount out number --总页数 <br> * ); <br> * end package_page; <br> * / <br> * * CREATE OR REPLACE Package Body package_page Is <br> * Procedure proc_page( <br> * p_sql varchar2, --查询的sql <br> * p_pagesize Number, --每页大小 <br> * p_curpage Number, --当前页 <br> * p_rowcount out Number, --总条数,输出参数 <br> * p_pagecount out number --总页数 <br> * ) <br> * is <br> * v_count_sql varchar2(2000); <br> * begin <br> * --查询总条数 <br> * v_count_sql := 'select count(*) from (' || p_sql || ')'; <br> * * --执行查询,查询总条数 <br> * execute immediate v_count_sql into p_rowcount; <br> * * dbms_output.put_line('查询总条数SQL=>'||v_count_sql); <br> * dbms_output.put_line('查询总条数Count='||p_rowcount); <br> * * --得到总页数 <br> * if mod(p_rowcount,p_pagesize)=0 <br> * then p_pagecount:=p_rowcount/p_pagesize; <br> * else p_pagecount:=p_rowcount/p_pagesize+1; <br> * end if; <br> * dbms_output.put_line('查询页数='||p_pagecount); <br> * end proc_page; <br> * end package_page; <br> * */ public static void pc() { System.out.println("这个方法的注释显示了存储过程......"); } /** * 得到上一次访问的URL request.getRequestURI().toString(); * */ private String getParamUrl() { String url = ""; url = this.request.getRequestURI().toString(); if (url.indexOf("?") == -1) { url = url + "?"; } String totalParams = ""; Enumeration params = this.request.getParameterNames();// 得到所有参数名 while (params.hasMoreElements()) { String tempName = params.nextElement().toString(); String tempValue = this.request.getParameter(tempName); if (tempValue != null && !tempValue.equals("") && !tempName.equals("currentpage") && !tempName.equals("extmsg")) { if (totalParams.equals("")) { totalParams = totalParams + tempName + "=" + tempValue; } else { totalParams = totalParams + "&" + tempName + "=" + tempValue; } } } String totalUrl = url + totalParams; return totalUrl; } /** * 得到页面的字符串! * */ private String getsimplePage() { StringBuffer str = new StringBuffer(); String url = this.getParamUrl(); int ProPage = this.currentpage - 1; int Nextpage = this.currentpage + 1; // 文字的分页 str.append("<form method='post' name='pageform' action=''>"); str .append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>"); str.append("<tr>"); str.append("<td width='3%'> </td>"); str.append("<td height='26'>"); str.append("共有记录" + this.rowCount + "条   "); str.append("共" + this.pageCount + "页   "); str.append("每页" + this.pageSize + "记录   "); str.append("第" + this.currentpage + "页/共" + this.pageCount + "页"); str.append("</td><td>"); if (this.currentpage > 1) { str.append("<a href="" + url + "¤tpage=1" mce_href="" + url + "&currentpage=1">首页</a>"); str.append("   "); str.append("<a href="" + url + "¤tpage=" + ProPage + "" mce_href="" + url + "&currentpage=" + ProPage + "">上一页</a>"); str.append("   "); } else { str.append("首页"); str.append("   "); str.append("上一页"); str.append("   "); } if (this.currentpage < this.pageCount) { str.append("<a href="" + url + "¤tpage=" + Nextpage + "" mce_href="" + url + "&currentpage=" + Nextpage + "">下一页</a>"); str.append("   "); } else { str.append("下一页"); str.append("   "); } if (this.pageCount > 1 && this.currentpage != this.pageCount) { str.append("<a href="" + url + "¤tpage=" + pageCount + "" mce_href="" + url + "&currentpage=" + pageCount + "">尾页</a>"); str.append("   "); } else { str.append("尾页"); str.append("   "); } str.append("转到"); str .append("<select name='currentpage' onchange='javascript:ChangePage(this.value);'>"); for (int j = 1; j <= pageCount; j++) { str.append("<option value='" + j + "'"); if (currentpage == j) { str.append("selected"); } str.append(">"); str.append("" + j + ""); str.append("</option>"); } str.append("</select>页"); str.append("</td><td width='3%'> </td></tr></table>"); str.append("<mce:script language='javascript'><!-- "); str.append("function ChangePage(testpage){"); str.append("document.pageform.action='" + url + "&currentpage='+testpage+'';"); str.append("document.pageform.submit();"); str.append("}"); str.append(" // --></mce:script>"); str.append("</form>"); return str.toString(); } }

 

示例:

package com.jungle.action; import java.sql.Connection; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.hibernate.jdbc.Work; import org.springframework.orm.hibernate3.HibernateTemplate; import com.jungle.utils.MyPageTool; public class UserAction { private HibernateTemplate hibernateTemplate; private Connection conn; private String page; public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); StringBuffer condition = new StringBuffer("where userid=1"); hibernateTemplate.getSessionFactory().openSession().doWork(new Work() { public void execute(Connection connection) throws SQLException { conn = connection; } }); int currentpage = 1; int pagesize = 3; String sql = "select * from (select t.*,rownum rn from t_user t " + condition + ") where 1=1"; MyPageTool mypage = new MyPageTool(request, conn, sql, currentpage, pagesize); page = mypage.getPageString(); sql += " and rn>" + pagesize * (currentpage - 1) + " and rn<=" + pagesize * currentpage; System.out.println(sql); return "success"; } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public Connection getConn() { return conn; } public void setConn(Connection conn) { this.conn = conn; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } }

 

你可能感兴趣的:(JavaScript,sql,exception,String,url,工具)