jdbc分页操作
2016-01-20 17:46
253 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zjx_Tenderness/article/details/50550046
UserInfoDao
private int pageSize = 10;// 每页记录数
// 获取总条数/总页数 public int count() throws SQLException{ int totalPages = 0;//总页数totalPages Connection connection = manager.getConnection(); try{ String sql="select count(1) from "+TABLE_NAME; PreparedStatement queryPreStatement=connection.prepareStatement(sql); ResultSet rs=queryPreStatement.executeQuery(); if(rs.next()){ int rowCount = rs.getInt(1); totalPages=rowCount%pageSize==0?rowCount/pageSize:rowCount/pageSize+1; } }finally{ if(connection!=null)connection.close(); } return totalPages; }
mysql分页查询语句:select o.* from (select * from wyuser) o limit firstIndex,pageSize
oracle分页查询语句1:select * from(select a.*,ROWNUM rn from(select * from wyuser order by ID asc) a where ROWNUM<=(firstIndex+pageSize)) where rn>firstIndex
oracle分页查询语句2:select * from(select * from(select t.*,row_number() over(order by orderColumn) as rownumber from(select * from wyuser) t) p where p.rownumber>firstIndex) where rownum<=pageSize
// sql语句分页查询 count=当前页 public List<UserInfo> page(int count) throws SQLException{ List<UserInfo> userInfoList = new ArrayList<UserInfo>(); Connection connection = manager.getConnection(); try { //String sql="select top "+pageSize+" * from "+TABLE_NAME+" where id>(select max (id) from (select top (("+count+"-1)*"+pageSize+") id from "+TABLE_NAME+" order by id) as T) order by id"; String sql1="select top "+pageSize+" * from "+TABLE_NAME+" where UserID not in(select top (("+count+"-1)*"+pageSize+") UserID from "+TABLE_NAME+" order by UserID) order by UserID"; Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(sql1); while (rs.next()) { UserInfo userInfo = new UserInfo(); userInfo.setUserID(rs.getInt("UserID")); userInfo.setUserName(rs.getString("UserName")); userInfo.setLastLoginDate(rs.getDate("LastLoginDate")); userInfo.setPoints(rs.getBigDecimal("Points")); userInfoList.add(userInfo); } rs.close(); st.close(); } finally { if (connection != null) { connection.close(); } } return userInfoList; }
// 修改积分 public int updatePoint(UserInfo userInfo) throws SQLException{ Connection connection = manager.getConnection(); try{ String sql="update "+TABLE_NAME+" set Points=? where UserID=?"; PreparedStatement updatePreStatement=connection.prepareStatement(sql); updatePreStatement.setBigDecimal(1, userInfo.getPoints()); updatePreStatement.setInt(2, userInfo.getUserID()); return updatePreStatement.executeUpdate(); }finally{ if(updatePreStatement!=null)updatePreStatement.close(); if(connection!=null)connection.close(); } }
// 根据用户名查找 public List<UserInfo> filterUser(String userName) throws SQLException{ List<UserInfo> userInfoList=new ArrayList<UserInfo>();; Connection connection = manager.getConnection(); try{ String sql="select * from "+TABLE_NAME+" where UserName='"+userName+"'"; Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(sql); //List<Map<String, Object>> result=this.query(sql); while (rs.next()) { 3ff7 UserInfo userInfo = new UserInfo(); userInfo.setUserID(rs.getInt("UserID")); userInfo.setUserName(rs.getString("UserName")); userInfo.setLastLoginDate(rs.getDate("LastLoginDate")); userInfo.setPoints(rs.getBigDecimal("Points")); userInfoList.add(userInfo); } // if(result!=null&&result.size()>0){ // Map<String, Object> row=result.get(0); // userInfo.setUserID((Integer)row.get("UserID")); // userInfo.setUserName((String)row.get("UserName")); // userInfo.setLastLoginDate((Date)row.get("LastLoginDate")); // userInfo.setPoints((BigDecimal)row.get("Points")); // } return userInfoList; }finally{ if(connection!=null)connection.close(); } }
// 根据id获取 public UserInfo get(int id) throws SQLException{ UserInfo userInfo=null; Connection connection = manager.getConnection(); try{ String sql="select * from "+TABLE_NAME+" where UserID=?"; PreparedStatement queryPreStatement=connection.prepareStatement(sql); queryPreStatement.setInt(1, id); ResultSet rs=null; rs=queryPreStatement.executeQuery(); userInfo=new UserInfo(); while(rs.next()){ userInfo.setUserID(rs.getInt("UserID")); userInfo.setUserName(rs.getString("UserName")); userInfo.setLastLoginDate(rs.getDate("LastLoginDate")); userInfo.setPoints(rs.getBigDecimal("Points")); break; } return userInfo; }finally{ if(connection!=null)connection.close(); } }
UserInfoService
private UserInfoDao userInfoDao=new UserInfoDao(); // 更新 目前只更新积分 public int updatePoint(UserInfo userInfo) throws SQLException{ return userInfoDao.updatePoint(userInfo); } // 根据用户名查找 public List<UserInfo> filterUser(String userName) throws SQLException{ return userInfoDao.filterUser(userName); } // 根据id获取 public UserInfo get(int id) throws SQLException{ return userInfoDao.get(id); } // 获取总条数 public int count() throws SQLException{ return userInfoDao.count(); } /** * sql语句分页查询 * @param count 当前页码 * @return * @throws SQLException */ public List<UserInfo> page(int count) throws SQLException{ return userInfoDao.page(count); }
userInfoServlet
/** * @see HttpServlet#HttpServlet() */ public UserInfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cmd=request.getParameter("cmd"); response.setCharacterEncoding("utf-8"); if(cmd==null)return; else if (cmd.equals("page")){ this.page(request,response); } else if (cmd.equals("filterUser")){ this.filterUser(request,response); } else if (cmd.equals("updatePoint")){ this.updatePoint(request,response); } else if (cmd.equals("save")){ this.save(request,response); } // else if(cmd.equals("new")) { // request.setAttribute("action", "add"); // request.getRequestDispatcher("/user/form.jsp").forward(request, response); // return; // }else if(cmd.equals("add")) { // Charset.defaultCharset(); // request.setCharacterEncoding("UTF-8"); // this.add(request, response); // } }
// 更新 目前只更新积分 private void updatePoint(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); if(id==null){ request.setAttribute(Constants.PAGE_MESSAGE_KEY, "参数不能为空。"); }else{ UserInfoService userInfoService=new UserInfoService(); try { UserInfo userInfo=userInfoService.get(Integer.parseInt(id)); request.setAttribute("action", "save"); request.setAttribute("userInfo", userInfo); request.getRequestDispatcher("/userInfo/form.jsp").forward(request, response); return; } catch (SQLException e) { e.printStackTrace(); request.setAttribute(Constants.PAGE_MESSAGE_KEY, "数据读取错误。"); } } response.sendRedirect(request.getContextPath()+"/UserInfoServlet?cmd=page&count=1"); }
// sql语句分页查询 private void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserInfoService userInfoService=new UserInfoService(); try { int count = Integer.parseInt(request.getParameter("count")); List<UserInfo> userInfos=userInfoService.page(count); request.setAttribute("datas", userInfos); request.setAttribute("currentPage", count); request.setAttribute("totalPages", userInfoService.count()); } catch (SQLException e) { e.printStackTrace(); request.setAttribute(Constants.PAGE_MESSAGE_KEY, "数据加载失败。"); } request.getRequestDispatcher("/userInfo/list.jsp").forward(request, response); }
// 根据用户名查找 private void filterUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserInfoService userInfoService=new UserInfoService(); try { String filter_text = request.getParameter("filter_text"); List<UserInfo> userFilter=userInfoService.filterUser(filter_text); request.setAttribute("datas", userFilter); } catch (SQLException e) { e.printStackTrace(); request.setAttribute(Constants.PAGE_MESSAGE_KEY, "数据加载失败。"); } request.getRequestDispatcher("/userInfo/list.jsp").forward(request, response); }
// 保存 private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); String points = request.getParameter("points"); BigDecimal bigDecimal=new BigDecimal(points); UserInfoService userInfoService=new UserInfoService(); try { UserInfo userInfo=userInfoService.get(Integer.parseInt(id)); userInfo.setPoints(bigDecimal); userInfoService.updatePoint(userInfo); } catch (SQLException e) { request.setAttribute(Constants.PAGE_MESSAGE_KEY, "用户添加失败。"); } response.sendRedirect(request.getContextPath()+"/UserInfoServlet?cmd=page&count=1"); }
list.jsp
<body> <div class="row"> <div class="col-xs-12"> <div class="table-responsive"> <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid"> <div class="row"> <div class="col-sm-4"> <form id="inputForm" action="${ctx}/UserInfoServlet?cmd=filterUser" method="post" class="form-horizontal"> <div class="input-group"> <input class="form-control" type="text" id="filter_text" name="filter_text"> <span class="input-group-btn"> <button id="sel_appointment_button" class="btn btn-sm btn-default" type="submit"> <i class="icon-search bigger-110"></i>用户名查找 </button> </span> </div> </form> </div> <div class="col-sm-1"> <div id="sample-table-2_info" class="dataTables_info"> <a class="btn btn-primary" href="${ctx}/UserInfoServlet?cmd=new"><i class="icon-plus"></i>添加</a> </div> </div> <div class="col-sm-1"> <div id="sample-table-2_info" class="dataTables_info"> <a class="btn btn-primary" href="${ctx}/UserInfoServlet?cmd=emptyPoints"><i class="icon-plus"></i>清空积分CompanyID=5000001</a> </div> </div> </div> <table id="contentTable" class="table table-striped table-bordered table-condensed"> <thead> <tr> <th>用户ID</th> <th>用户名称</th> <th>最后登录时间</th> <th width="280">操作</th> </tr> </thead> <tbody> <c:forEach items="${datas}" var="item"> <tr> <td>${item.userID}</td> <td>${item.userName}</td> <td>${item.lastLoginDate}</td> <td> <a id="a_updatePoint" class="btn btn-xs btn-info" href="${ctx}/UserInfoServlet?cmd=updatePoint&id=${item.userID}"> 修改积分 </a> <%-- <a id="a_delete" class="btn btn-xs btn-danger" href="${ctx}/pc/user?cmd=delete&id=${item.userID}"> 删除 </a> --%> </td> </tr> </c:forEach> </tbody> </table> <div class="row"> <div class="col-sm-12"> <a href="${ctx}/UserInfoServlet?cmd=page&count=1">首页</a> <c:if test="${currentPage>1}"> <a href="${ctx}/UserInfoServlet?cmd=page&count=${currentPage-1}">上一页</a> </c:if> 第${currentPage}页 <c:if test="${currentPage<totalPages}"> <a href="${ctx}/UserInfoServlet?cmd=page&count=${currentPage+1}">下一页</a> </c:if> <a href="${ctx}/UserInfoServlet?cmd=page&count=${totalPages}">尾页</a> </div> </div> </div> </div> </div> </div> </body>
form.jsp
<body> <div class="row"> <div class="col-xs-12"> <form id="inputForm" action="${ctx}/UserInfoServlet?cmd=${action}" method="post" class="form-horizontal" enctype="application/x-www-form-urlencoded"> <input type="hidden" name="id" value="${userInfo.userID}" /> <fieldset> <div class="form-group"> <label for="name" class="col-sm-3 control-label no-padding-right">积分:</label> <div class="col-sm-9"> <input type="text" id="points" name="points" value="${userInfo.points}" /> </div> </div> <div class="clearfix form-actions"> <div class="col-md-offset-3 col-md-9"> <button id="submit_btn" class="btn btn-danger" type="submit"> <i class="icon-ok bigger-110"></i> 提交 </button> <button id="cancel_btn" class="btn btn-info" type="button" οnclick="history.back()"> <i class="icon-reply icon-only"></i> 返回 </button> </div> </div> </fieldset> </form> </div> </div> </body>
相关文章推荐
- JSP数据库操作(JDBC-ODBC) - 分页显示
- JDBC操作 SQL SERVER之数据的分页显示
- JDBC操作 SQL SERVER之数据的分页显示
- 第八篇——JDBC操作数据库之分页查询
- jdbc结合sqlserver的javaWeb工程的分页查询共通操作代码
- JSP+JDBC实现的分页操作
- (转)JSP数据库操作例程 - 数据分页显示 - JDBC 2.0 - Oracle
- JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
- JDBC操作数据库之分页查询
- JDBC(2)JAVA连接数据库之 分页操作、事务操作、代理模式
- 利用jdbc对数据表进行分页,操作存储过程(有参与无参,返回游标),函数等
- 通过JDBC连接操作oracle数据库的十大技巧
- JDBC连接数据库通用操作
- 使用JDBC进行后台登录和注册操作
- 开源类库操作excel文章,分页导出数据等等
- Java数据库连接——JDBC基础知识(操作数据库:增删改查)【转】
- JDBC操作数据库基本封装
- JDBC-数据库的连接和简单操作
- C#数据库操作(阅读器、数据库绑定、修改、统计、分页)
- JDBC java对MySQL数据库进行查询操作,并把查询的结果输出