zoukankan      html  css  js  c++  java
  • oracle实现分页总结

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8" import="java.sql.*"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Oracle和Jsp的分页程序</title>

    </head>

    <body>

     

    <%

    out.print("<table>");

      int pageSize =3;//每页显示3条

      int pageCount = 0;//总页数

      int intRowCount = 0;//总记录数,可通过查询得到总得多少行

      int currentPage =1;

     

    try {

             Class.forName("oracle.jdbc.driver.OracleDriver");

             Connection conn = DriverManager.getConnection(

                                "jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "scott", "tiger");

             Statement st = conn.createStatement();

            

             ResultSet rs2 = st.executeQuery("select count(*) totalRows from emp");//总记录数

             out.print("<tr>");

             if(rs2.next()){

                      

                       intRowCount=rs2.getInt("totalrows");

                       out.print("<td>"+"总记录数是:"+intRowCount+"</td>");

             }

             out.print("</tr>");

            

             if(intRowCount%pageSize==0){

                       pageCount=intRowCount/pageSize;

             }else{

                       pageCount=intRowCount/pageSize+1;

             }

            

            

             out.print("<tr><td>总共有"+ pageCount+"页</td></tr>");

             String StrPage=request.getParameter("page");

            

             if(StrPage==null){

        currentPage=1;                

             }else{

                       currentPage=Integer.parseInt(StrPage);

                       if(currentPage<1) currentPage=1;

             }

     

     

             out.print("<tr><td>姓名</td><td>薪水</td></tr>");

             ResultSet rs = st.executeQuery("select a2.* from (select a1.*, rownum rn from (select ename,sal from emp) a1 where rownum<="+currentPage*pageSize+") a2 where a2.rn>="+(pageSize*(currentPage-1)+1)+" ");

             while(rs.next()){

                      

                       out.print("<tr>");

                       out.print("<td>"+rs.getString("ename")+"</td>");

                       out.print("<td>"+rs.getString("sal")+"</td>");

                       out.print("</tr>");

             }

            

             out.print("</table>");

             for( int i=1;i<=pageCount;i++){

                       out.print("<a  href=myoracle.jsp?page="+i+">["+ i+"]</a>");

             }

             out.print("<tr><td>现在是第"+ StrPage+"页</td></tr>");

             conn.close();

             st.close();

             rs.close();

    } catch (Exception e) {

             e.printStackTrace();

    }

    %>

    </table>

     

    </body>

    </html>

  • 相关阅读:
    Bit Manipulation
    218. The Skyline Problem
    Template : Two Pointers & Hash -> String process
    239. Sliding Window Maximum
    159. Longest Substring with At Most Two Distinct Characters
    3. Longest Substring Without Repeating Characters
    137. Single Number II
    142. Linked List Cycle II
    41. First Missing Positive
    260. Single Number III
  • 原文地址:https://www.cnblogs.com/kunpengit/p/2277640.html
Copyright © 2011-2022 走看看