列表分页(楼上)

    技术2022-05-12  12

    BaseBO中的 方法queryList()

     

    protected Vector queryList(String querySql, int rowNumEachPage, int currentPage) { System.out.println("baseBoSql:"+querySql); if(rowNumEachPage == 0) return queryList(querySql); if(dsf == null) dsf = DataSourceFactory.create(); if(dsf == null) { setMessage("对不起,创建数据源工厂对象失败。"); return null; } Connection conn = dsf.getConnection(); if(conn == null) { setMessage("对不起,无法获取数据库连接。"); return null; } Hashtable pageData = new Hashtable(); int totalRowNum = 0; PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData rsmt = null; Vector data = new Vector(); try { Vector vector1; try { conn.setAutoCommit(true); conn.setReadOnly(true); ps = conn.prepareStatement(querySql, 1004, 1007); rs = ps.executeQuery(); rsmt = rs.getMetaData(); rs.last(); totalRowNum = rs.getRow(); int end = currentPage * rowNumEachPage; int start = end - rowNumEachPage; if(currentPage <= 0 || start >= totalRowNum) { currentPage = 1; end = currentPage * rowNumEachPage; start = end - rowNumEachPage; } if(start == 0) rs.beforeFirst(); else rs.absolute(start); Hashtable ht; float jinef=0; for(; rs.next() && start++ < end; data.add(ht)) { ht = new Hashtable(); for(int i = 1; i <= rsmt.getColumnCount(); i++) { int columnType = rsmt.getColumnType(i); String value = rs.getString(i); ht.put(rsmt.getColumnName(i).toUpperCase(), value != null ? ((Object) (value)) : ""); if(rsmt.getColumnName(i).equals("SPRIX")){ if(value !=null ){ jinef = jinef + Float.valueOf(value).floatValue(); } } } } System.out.println("JINEF:"+jinef); pageData.put("JINEF", String.valueOf(jinef/10000)); pageData.put("TOTALROWNUM", String.valueOf(totalRowNum)); pageData.put("ROWNUMEACHPAGE", String.valueOf(rowNumEachPage)); String total = null; if(totalRowNum % rowNumEachPage == 0) total = String.valueOf(totalRowNum / rowNumEachPage); else total = String.valueOf(totalRowNum / rowNumEachPage + 1); pageData.put("TOTALPAGENUM", total); pageData.put("CURRENTPAGE", String.valueOf(currentPage)); setPageData(pageData); Vector vector = data; return vector; } catch(Exception e) { System.out.println("Sql语句执行错误:" + e.getMessage()); System.out.println("错误的Sql语句是:" + querySql); setMessage("对不起,数据库查询失败。//n" + e.getMessage()); vector1 = null; } return vector1; } finally { try { if(ps != null) ps.close(); } catch(SQLException e) { System.out.println("关闭prepareStatement对象时错误:" + e.getMessage()); } try { if(rs != null) rs.close(); } catch(SQLException e) { System.out.println("关闭ResultSet对象时错误:" + e.getMessage()); } try { if(conn != null) conn.close(); } catch(SQLException e) { System.out.println("关闭Connection对象时错误:" + e.getMessage()); } } }

     

     

    LogBo中方法 引用

    public Vector getLogList(int count, int nowpage) { String sql = "select * from LOG4tkdm04 order by LOGID DESC"; return queryList(sql, count, nowpage); }

     

     

     

     

    logQueryCmd中引用

    import com.tkdm04.bo.SysManage.clientBO; import com.tkdm04.bo.log.LogBO; import com.tkdm04.cmd.BaseCommand; import com.tkdm04.view.sysmanage.log.logInfoListView; public class logQueryCommand extends BaseCommand { private int count; public logQueryCommand() { count = 14; } public boolean execute() { System.out.println("logQueryCommand begin!"); LogBO bo = new LogBO(); //表名称 String table ="LOG4tkdm04"; // 当前页数 int nowpage = 1; if(getRequest().getParameter("nowpage") != null) nowpage = Integer.parseInt(getRequest().getParameter("nowpage")); logInfoListView list = (logInfoListView)getListView(); list.setDataListByHashtable(bo.getLogList(count, nowpage), getPrimaryName()); list.setPageData(bo.getPageData()); return true; } public String getListViewName() { return "logInfoListView"; } public String getPrimaryName() { return ""; } public String getViewName() { return ""; } }

     

     

     

    执行Cmd后进入页面

    <jsp:useBean id="logInfoListView" class="com.tkdm04.view.sysmanage.log.logInfoListView" scope="session"/> ········ ········ <!-- 以下是分页 --> <table cellpadding=0 cellspacing=3 border=0 width="100%" align=center> <tr> <td valign=middle align="right" width="20%"> 页数:(<%=logInfoListView.getCurrentPage()%>/<%=logInfoListView.getTotalPageNum()%>)   <%if(logInfoListView.getCurrentPage()==1){%> <font color="gray">首页</font> <%} else{ %> <a href="javascript:gotofirstpage(0,'log4Tkdm04List','logQueryCmd','true');" mce_href="javascript:gotofirstpage(0,'log4Tkdm04List','logQueryCmd','true');">首页</a> <%}%>   <%if(logInfoListView.getCurrentPage()==0||logInfoListView.getCurrentPage()==1){%> <font color="gray">上一页</font> <%} else{ %> <a href="javascript:uppage(0,'log4Tkdm04List','logQueryCmd','true');" mce_href="javascript:uppage(0,'log4Tkdm04List','logQueryCmd','true');">上一页</a> <%}%>   <%if(logInfoListView.getCurrentPage()==logInfoListView.getTotalPageNum()||logInfoListView.getTotalPageNum()==0){%> <font color="gray">下一页</font> <%} else{ %> <a href="javascript:nextpage(0,'log4Tkdm04List','logQueryCmd','true')" mce_href="javascript:nextpage(0,'log4Tkdm04List','logQueryCmd','true')">下一页</a> <%}%>   <%if(logInfoListView.getCurrentPage()==logInfoListView.getTotalPageNum()||logInfoListView.getTotalPageNum()==0){%> <font color="gray">末页</font> <%} else{ %> <a href="javascript:gotolastpage(0,'log4Tkdm04List','logQueryCmd','true','<%=logInfoListView.getTotalPageNum()%>');">末页</a> <%}%>    <input type="text" size=3 name="gopage" οnkeydοwn="if(event.keyCode==13) {return false;}"> <input type="button" value="go" onClick="javascript:gotopage(0,'log4Tkdm04List','logQueryCmd','true');"> </td> </tr> </table> ········ ········ <!--当前页--> <input type="hidden" name="nowpage" value="<%=logInfoListView.getCurrentPage()%>"> <!--共多少页--> <input type="hidden" name="allpage" value="<%=logInfoListView.getTotalPageNum()%>">

     

     

    页面引用 <SCRIPT Language="javascript" src="resource1/js/gwp.js"></SCRIPT>

    在JS代码中加入

    //=======(分页显示)**开始**======= //上一页 function uppage(n,nextpage,command,canexecute){ var now=document.all.item("nowpage").value; if(now!=null){ var up=parseInt(now); if(!isNaN(up)){ if(now-1<=0){ up=1; }else{ up=now-1; } document.all.item("nowpage").value=up; pageshow(n,nextpage,command,canexecute); } } } //下一页 function nextpage(n,nextpage,command,canexecute){ //alert("aa"); var now=document.all.item("nowpage").value; //alert(now); var all=document.all.item("allpage").value; //alert(all); if(now!=null&&all!=null){ var next=parseInt(now); if(!isNaN(next)){ if(!isNaN(parseInt(all))){ if(next+1>parseInt(all)){ next=parseInt(all); }else{ next+=1; } document.all.item("nowpage").value=next; pageshow(n,nextpage,command,canexecute); } } } } //至第几页 function gotopage(n,nextpage,command,canexecute){ var now=document.all.item("nowpage").value; var all=document.all.item("allpage").value; var go=document.all.item("gopage").value; if(!isNaN(parseInt(go))){ if(go!=null&&now!=null&&all!=null){ if(!isNaN(parseInt(all))&&parseInt(go)>parseInt(all)){ document.all.item("nowpage").value=parseInt(all); pageshow(n,nextpage,command,canexecute); return; } if(parseInt(go)<1){ document.all.item("nowpage").value="1"; pageshow(n,nextpage,command,canexecute); return; } document.all.item("nowpage").value=parseInt(go); pageshow(n,nextpage,command,canexecute); } } } //首页 function gotofirstpage(checkvalue,nextpage,command,canexecute){ document.all.item("nowpage").value="1"; pageshow(checkvalue,nextpage,command,canexecute); return; } //末页 function gotolastpage(checkvalue,nextpage,command,canexecute,lastpage){ //alert('lastpage='+lastpage); document.all.item("nowpage").value=lastpage; pageshow(checkvalue,nextpage,command,canexecute); return; } //=======(分页显示)**结束**=======

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     


    最新回复(0)