标签:
/** * 分页查询 * @param pageNo 第几页 * @param pageSize 每页多少条数据 * @return pageModel */ public PageModel findUserList(int pageNo, int pageSize) { StringBuffer sbSql = new StringBuffer(); sbSql.append("select user_id, user_name, password, contact_tel, email, create_date ") .append("from ") .append("( ") .append("select rownum rn, user_id, user_name, password, contact_tel, email, create_date ") .append("from ") .append("( ") .append("select user_id, user_name, password, contact_tel, email, create_date from t_user where user_id <> ‘root‘ order by user_id ") .append(") where rownum <= ? ") .append(") where rn > ? "); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; PageModel pageModel = null; try { conn = DbUtil.getConnection(); pstmt = conn.prepareStatement(sbSql.toString()); pstmt.setInt(1, pageNo * pageSize); pstmt.setInt(2, (pageNo - 1) * pageSize); rs = pstmt.executeQuery(); List userList = new ArrayList(); while (rs.next()) { User user = new User(); user.setUserId(rs.getString("user_id")); user.setUserName(rs.getString("user_name")); user.setPassword(rs.getString("password")); user.setContactTel(rs.getString("contact_tel")); user.setEmail(rs.getString("email")); user.setCreateDate(rs.getTimestamp("create_date")); userList.add(user); } pageModel = new PageModel(); pageModel.setList(userList); pageModel.setTotalRecords(getTotalRecords(conn)); pageModel.setPageSize(pageSize); pageModel.setPageNo(pageNo); }catch(SQLException e) { e.printStackTrace(); }finally { DbUtil.close(rs); DbUtil.close(pstmt); DbUtil.close(conn); } return pageModel; }
前台jsp:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %> <%@ page import="com.bjpowernode.drp.sysmgr.domain.*" %> <%@ page import ="com.bjpowernode.drp.sysmgr.manager.*" %> <%@ page import ="com.bjpowernode.drp.util.*" %> <%@ page import="java.util.List" %> <% int pageNo=1; int pageSize=2; PageModel pageModel=UserManager.getInstance().findUserList(pageNo,pageSize); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>用户维护</title> <link rel="stylesheet" href="../style/drp.css"> <script type="text/javascript"> function addUser() { window.self.location = "user_add.jsp"; } function modifyUser() { window.self.location = "user_modify.html"; } function deleteUser() { //alert("删除");-- // TODO var selectFlags=document.getElementsByName("selectFlag"); for (var i=0;i<selectFlags.length;i++){ if(selectFlags[i].checked=false){ alert("请选中一条记录后删除"); } } } //选中所有-复选框 function checkAll(field) { //alert("Hello");] // var selectFlags=document.getElementsByName("selectFlag"); for (var i=0;i<selectFlags.length;i++){ selectFlags[i].checked=field.checked; } } function topPage() { } function previousPage() { } function nextPage() { } function bottomPage() { } </script> </head> <body class="body1"> <form name="userform" id="userform"> <div align="center"> <table width="95%" border="0" cellspacing="0" cellpadding="0" height="35"> <tr> <td class="p1" height="18" nowrap> </td> </tr> <tr> <td width="522" class="p1" height="17" nowrap> <img src="../images/mark_arrow_02.gif" width="14" height="14"> <b>系统管理>>用户维护</b> </td> </tr> </table> <hr width="100%" align="center" size=0> </div> <table width="95%" height="20" border="0" align="center" cellspacing="0" class="rd1" id="toolbar"> <tr> <td width="49%" class="rd19"> <font color="#FFFFFF">查询列表</font> </td> <td width="27%" nowrap class="rd16"> <div align="right"></div> </td> </tr> </table> <table width="95%" border="1" cellspacing="0" cellpadding="0" align="center" class="table1"> <tr> <td width="55" class="rd6"> <input type="checkbox" name="ifAll" onClick="checkAll(this)"> </td> <td width="119" class="rd6"> 用户代码 </td> <td width="152" class="rd6"> 用户名称 </td> <td width="166" class="rd6"> 联系电话 </td> <td width="150" class="rd6"> email </td> <td width="153" class="rd6"> 创建日期 </td> </tr> <% List userList= pageModel.getList(); for(Iterator iter=userList.iterator();iter.hasNext();){ User user=(User)iter.next(); %> <tr> <td class="rd8"> <input type="checkbox" name="selectFlag" class="checkbox1" value="<%=user.getUserId() %>"> </td> <td class="rd8"> <%=user.getUserId() %> </td> <td class="rd8"> <%=user.getUserName() %> </td> <td class="rd8"> <%=user.getContactTel() %> </td> <td class="rd8"> <%=user.getEmail() %> </td> <td class="rd8"> <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getCreateDate()) %> </td> </tr> <% } %> <tr> <td width="55" class="rd7"> </td> <td width="119" class="rd7" height="13"> </td> <td width="152" class="rd7"> </td> <td width="166" class="rd7"> </td> <td width="150" class="rd7"> </td> <td width="153" class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> <tr> <td class="rd7"> </td> <td class="rd7" height="13"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> <td class="rd7"> </td> </tr> </table> <table width="95%" height="30" border="0" align="center" cellpadding="0" cellspacing="0" class="rd1"> <tr> <td nowrap class="rd19" height="2"> <div align="left"> <font color="#FFFFFF"> 共 xx 页</font> <font color="#FFFFFF">当前第</font>  <font color="#FF0000">x</font>  <font color="#FFFFFF">页</font> </div> </td> <td nowrap class="rd19"> <div align="right"> <input name="btnTopPage" class="button1" type="button" id="btnTopPage" value="|<< " title="首页" onClick="topPage()"> <input name="btnPreviousPage" class="button1" type="button" id="btnPreviousPage" value=" < " title="上页" onClick="previousPage()"> <input name="btnNextPage" class="button1" type="button" id="btnNextPage" value=" > " title="下页" onClick="nextPage()"> <input name="btnBottomPage" class="button1" type="button" id="btnBottomPage" value=" >>|" title="尾页" onClick="bottomPage()"> <input name="btnAdd" type="button" class="button1" id="btnAdd" value="添加" onClick="addUser()"> <input name="btnDelete" class="button1" type="button" id="btnDelete" value="删除" onClick="deleteUser()"> <input name="btnModify" class="button1" type="button" id="btnModify" value="修改" onClick="modifyUser()"> </div> </td> </tr> </table> <p> </p> </form> </body> </html>
标签:
原文地址:http://www.cnblogs.com/wangmei/p/5022369.html