一个简单的JSP分页代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.Connection,java.sql.Statement,java.sql.ResultSet" %>
<jsp:directive.page import="java.sql.DriverManager;"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
 <%
Connection con=null;
//加载 Oracle jdbc  thin 驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
//Oracle thin jdbc URL
String url="jdbc:oracle:thin:@192.168.10.2:1521:ora10";
con = DriverManager.getConnection(url,"TRSPORTALV6","TRSPORTALV6");
 Statement stmt=con.createStatement();
 String sql="select count(*) from WCMMETATABLEXWLW_ZZH ";
 ResultSet rs=stmt.executeQuery(sql);
 int jumpPage=1;//跳转页
 int pageNum=1;//当前页
 int pageSize=3;//每页大小
 int countPage=0;//总页数
 int countRecord=0;//总记录数
 //总记录数
 if(rs.next()){
  countRecord=rs.getInt(1);
 }
 //总页数
if(countRecord % pageSize == 0){
  countPage = countRecord / pageSize;
}else{
   countPage = countRecord/pageSize+1;
}
 String strPage=request.getParameter("pageNum");
 String strjump=request.getParameter("jumpPage");
 if(strPage==null){
  pageNum=1;//如果没有则显示第一页
 }else{
  pageNum=Integer.parseInt(strPage);
  if(pageNum<1){
   pageNum=1;
  }
 }
 //对跳转页进行判断
 if(strjump!=null){
   pageNum=Integer.parseInt(strjump);
   if(pageNum<1){
    pageNum=1;
   }
   if(pageNum>countPage){
    pageNum=countPage;
   }
  }
 int startPage=(pageNum-1)*pageSize+1;
 int endPage=(pageNum)*pageSize;

//这SQL语句很重要,基本上用了ORACLE的rownum来实现分页的。
 sql="select * from (select t.*,rownum r from WCMMETATABLEXWLW_ZZH t) where r between "+startPage+" and "+endPage+"";
 rs=stmt.executeQuery(sql);
 while(rs.next()){
 %>
   <table>
    <tr><td><%=rs.getString("CRUSER")%></td><td><%=rs.getString("CRTIME")%></td></tr>
   </table>
   <%}%>
   <table>
   <form name="name" action="test.jsp">
    <tr>
    <td>总共有<%=countRecord%>条记录</td>
    <td>第<a href="test.jsp?pageNum=<%=pageNum%>"><%=pageNum%></a>页</td>
    <td>总<%=countPage%>页</a></td>
    <td><a href="test.jsp?pageNum=0">首页</a></td>
    <td><%if(pageNum>1){%><a href="test.jsp?pageNum=<%=pageNum-1%>">上页</a><%}%></td>
    <td><%if(pageNum<countPage){%><a href="test.jsp?pageNum=<%=pageNum+1%>">下页</a><%}%></td>
    <td><a href="test.jsp?pageNum=<%=countPage%>">尾页</a></td>
    <td>转到<input type="text" name="jumpPage" style="width: 15px">页<input type="submit" value="跳转" ></td>
    </tr>
    </form>
   </table>
  </body>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于JSP模糊查询分页的实现,可以采用以下简单代码实现: 首先,在jsp页面中,定义相应的查询表单,包括输入查询关键字和选择页码等必要的组件。 接着,在后台的servlet中,获取相应的查询关键字,并结合查询语句实现模糊查询,同时根据选择的页码计算出需要查询的数据起始位置和结束位置,从而实现数据分页。 最后,将查询结果及分页信息传送到jsp页面中,结合相应的页面模板实现数据展示和分页导航。 具体的代码实现可参考以下示例: 1. JSP页面: <form method="post" action="query.jsp"> <input type="text" name="keyword" placeholder="请输入查询关键字" required> <button type="submit">查询</button> </form> <%-- 数据展示 --%> <table> <thead> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <% for(int i=0; i<list.size(); i++){ %> <tr> <td><%=i+1 %></td> <td><%=list.get(i).getName() %></td> <td><%=list.get(i).getAge() %></td> <td><%=list.get(i).getGender() %></td> </tr> <% } %> </tbody> </table> <%-- 分页导航 --%> <% if(totalPage>0){ %> <div class="pagination"> <% if(curPage>1){ %> <a href="?keyword=<%=keyword %>&page=<%=curPage-1 %>" class="prev">«</a> <% } %> <% for(int i=1; i<=totalPage; i++){ %> <% if(i==curPage){ %> <span class="current"><%=i %></span> <% }else{ %> <a href="?keyword=<%=keyword %>&page=<%=i %>" class="page"><%=i %></a> <% } %> <% } %> <% if(curPage<totalPage){ %> <a href="?keyword=<%=keyword %>&page=<%=curPage+1 %>" class="next">»</a> <% } %> </div> <% } %> 2. Servlet代码String keyword = request.getParameter("keyword"); int curPage = request.getParameter("page")==null ? 1 : Integer.valueOf(request.getParameter("page")); int pageSize = 10; int startIndex = (curPage-1)*pageSize; int endIndex = curPage*pageSize-1; List<Data> list = dao.query(keyword, startIndex, endIndex); int totalCount = dao.count(keyword); int totalPage = (totalCount-1)/pageSize+1; request.setAttribute("list", list); request.setAttribute("keyword", keyword); request.setAttribute("curPage", curPage); request.setAttribute("totalPage", totalPage); request.getRequestDispatcher("query.jsp").forward(request, response); 3.DAO代码: public List<Data> query(String keyword, int startIndex, int endIndex){ String sql = "select * from data where name like ? limit ?,?"; Object[] params = new Object[]{ '%'+keyword+'%', startIndex, endIndex }; return template.query(sql, new BeanPropertyRowMapper<Data>(Data.class), params); } public int count(String keyword){ String sql = "select count(*) from data where name like ?"; Object[] params = new Object[]{ '%'+keyword+'%' }; return template.queryForObject(sql, Integer.class, params); } 需要注意的是,该示例代码使用了JavaWeb的常用组件,如servlet、JDBCJSP等,同时也使用了一些常用的第三方jar包,如spring-jdbc和mysql-connector-java等,因此在使用前需确保相关环境和依赖都已配置完毕。同时,由于该示例只是展示了一种可能的实现方式,如果需要满足更多的实际需求,还需根据具体情况进行相应的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值