oracle分页显示的servlet例子——相比mysql简单多了,哈哈!

<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body>
  <%
   int pageCount = 1;//总页数,默认为1
   int pageSize = 2;//每页显示多少条记录
   int rowCount = 1;//总共有多少条记录,默认为1
   String temp = request.getParameter("pageNow");
 int pageNow = 1;
   if(temp!=null){
    pageNow = Integer.parseInt(temp);
   }
   else{
    pageNow = 1;
   }
   
    Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORACLE","scott","tiger");
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("select a2.* from(select a1.*,rownum rn from(select * from emp) a1 where rownum<="+(pageSize+(pageNow-1)*pageSize)+")a2 where rn>="+(1+(pageNow-1)*pageSize)+"");
 while(rs.next()){
  %>
  姓名:<%=rs.getString(2) %><br>
  
 <%
 }


 
 rs = stmt.executeQuery("select count(1) from emp");
 if(rs.next())
 {
  rowCount = Integer.parseInt(rs.getString(1));
 }
 out.println("<br>总记录数"+rowCount+"<br>");
 pageCount = (rowCount/pageSize==0) ?(rowCount/pageSize):(rowCount/pageSize+1);
 out.println("<br>总页码数"+pageCount+"<br>");
 %>
 当前第<select name="11" id="11" disabled=true > 
 <%
 
  %>
    <option value="+<%=pageNow %>+"><%=pageNow %></option>
  <%
 
 %>
  </select>/总共[<%=pageCount %>]页
 <%
 
 for(int i=1;i<=pageCount;i++){
  %>
  [<a href='page.jsp?pageNow=<%=i %>'><%=i %></a>]
  <%
 }
 stmt.close();
 conn.close();
  %>
  


  </body>
</html>

 

PS:还有个小小的BUG,就是会多出一页,最后一页会出现没数据的情况。

你可能感兴趣的:(oracle分页显示的servlet例子——相比mysql简单多了,哈哈!)