oracle数据库中分页查询

最近整理了有关用jsp实现oracle数据库分页的两种方法,贴出来与大家交流一下。

第一种是用jsp实现oracle数据库分页。

    我的根目录是G:\workspace\hope\WebContent

    2.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页显示</title>

<% Connection con=null;
   ResultSet rs=null;
   Statement stmt=null;
   String strSQL="";
   int PageSize=2;
   int PageAllCount=1;
   int Page=1;
   int RowCount=0;
   try
   {
    Class.forName("oracle.jdbc.driver.OracleDriver");
   }
   catch(ClassNotFoundException classnotfoundexception)
   {
       System.out.println(classnotfoundexception.getMessage());
   }
   try{
    con=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:jh","jh","jh");
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    strSQL="select * from student";
    rs=stmt.executeQuery(strSQL);
    if(rs.next()){
     rs.last();  
     RowCount=rs.getRow();
     System.out.println(RowCount);
    }
    if(RowCount%PageSize==0)
     PageAllCount=RowCount/PageSize;
    else
     PageAllCount=(RowCount+PageSize-1)/PageSize;
    if(PageAllCount==0) PageAllCount=1;
    if(request.getParameter("Page")==null||request.getParameter("Page").equals(""))
     Page=1;
    else
     try{
      Page=Integer.parseInt(request.getParameter("Page"));
        }
    catch(java.lang.NumberFormatException e){
    Page=1;
   }
   if(Page<1) Page=1;
   if(Page>PageAllCount) Page=PageAllCount;
   rs.absolute((Page-1)*PageSize+1);
  
   out.print("<TABLE BORDER='1'>");
  
    for(int iPage=1;iPage<=PageSize;iPage++){
    out.println("<TR>");  
    out.println("<TD>"+rs.getString("no")+"</TD>");   
    out.println("<TD>"+rs.getString("name")+"</TD>");
    out.println("<td>"+rs.getFloat("math")+"</td>");
    out.println("<td>"+rs.getFloat("english")+"</td>");
    out.println("<td>"+rs.getFloat("chinese")+"</td>");
    out.println("</TR>");
   if(!rs.next()) break;
    }
    out.print("</TABLE>");
        
    }
    catch(SQLException e1){e1.printStackTrace();}
    finally{
     stmt.close();
     con.close();
    }
%>
</head>
<body>

<FORM action="2.jsp" method="get">
<% if(Page!=1){
 out.print(" <A herf=2.jsp?Page=1>第一页</A>");
 out.print(" <A herf=2.jsp?Page="+(Page-1)+">上一页</A>");
}
   if(Page!=PageAllCount){
  out.print(" <A herf=2.jsp?Page="+(Page+1)+">下一页</A>");
  out.print(" <A herf=2.jsp?Page="+PageAllCount+">最后一页</A>");
   }
%>
<BR>输入页数:<input TYPE="TEXT" name="Page" size="3">
页数:<font color="black"><%=Page%>/<%=PageAllCount%></font>
</FORM>


第<%=Page%>页 共<%=PageAllCount%>页
<br>
<a href="2.jsp?Page=1">首页</a>
<%if(Page<PageAllCount){%><a href="2.jsp?Page=<%=Page+1%>">下一页</a><%}%>
<%if(Page>1){%><a href="1.jsp?Page=<%=Page-1%>">上一页</a><%}%>
<a href="2.jsp?Page=<%=PageAllCount%>">尾页</a>
</br>
</body>
</html>

第二种是用jsp和javaBean实现oracle数据库分页。

1.jsp代码如下:

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="DBLink" scope="page" class="flower.grass.ShowRecordByPage"/>
<%
//变量声明
     String mysql=new String(); //SQL语句
     int RowCount=0;  //总的记录数
     int PageAllCount=0; //总的页数
     int PageSize=2; //每页显示的记录数
     int showPage; //待显示页码
     String strPage=new String(); //用来接收当页码参数
     int begin_no=0; //开始的rownum记录号
     int end_no=0;  //结束的rownum记录号
      //sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
     //取得待显示页码
     strPage = request.getParameter("page");
     if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
         showPage = 1;
         }
     else{//将字符串转换成整型
         showPage = Integer.parseInt(strPage);
         if(showPage<1) showPage = 1;
         System.out.println(showPage);
}


//得到总的数据记录行数
      mysql="select count(*) total_rows from student";
 //rs=sql.executeQuery("select * from (select rownum r,no,name,math,english,chinese from student where rownum<=5)t where t.r>=3");
  /*if(rs.next()){
    rs.last();
    RowCount=rs.getRow();   
  }   */
  ResultSet rs=DBLink.executequery(mysql);
   if(rs.next())
      {
      RowCount=rs.getInt("total_rows"); //这里只能用getInt()
      out.print("Total rows is:"+RowCount);
        }
 
   rs.close();

//计算总共要分多少页
     PageAllCount = (RowCount+PageSize-1)/PageSize;
//调整待显示的页码
     if(showPage>PageAllCount) showPage = PageAllCount;
//out.print("<br>Total pages is:"+intPageCount);

%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP数据库操作例程 - 数据分页显示 - JDBC 2.0 - Oracle</title>
</head>
<body>
<FONT size=3>

<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>学号</th>
<th>姓名</th>
<th>语文</th>
<th>数学</th>
<th>英语</th>
</tr>

<%
    begin_no=(showPage-1) * PageSize + 1;
    end_no = showPage * PageSize;
 //out.print("<br>begin:"+begin_no+"<br>end:"+end_no);
   mysql="select * from (select rownum r,no ,name,math,chinese,english from (select no,name,math,chinese,english from student)) where r between " +begin_no+ " and " +end_no;
   rs=DBLink.executequery(mysql);
   // rs=DBLink.executequery("select * from (select rownum r,no,name,math,chinese,english from student where rownum<=5)t where t.r>=1");
  while(rs.next())
 {
   %>
   <tr>
       <td><%=rs.getString("no")%></td>
       <td><%=rs.getString("name")%></td>
       <td><%=rs.getFloat("math")%></td>
       <td><%=rs.getFloat("english")%></td>
       <td><%=rs.getFloat("chinese")%></td> 
      </tr>
   <%
      }
   rs.close();
   %>
</table>

第<%=showPage%>页 共<%=PageAllCount%>页
<br>
<a href="1.jsp?page=1">首页</a><%if(showPage<PageAllCount){%><a href="1.jsp?page=<%=showPage+1%>">下一页</a><%}%>
<%if(showPage>1){%><a href="1.jsp?page=<%=showPage-1%>">上一页</a><%}%>
<a href="1.jsp?page=<%=PageAllCount%>">尾页</a>
</br><%
 //关闭数据库连接
   DBLink.closecon();%>

</FONT>
</body>
</html>
ShowRecordByPage.java放在G:\workspace\hope\src\flower\grass下,代码如下:

package flower.grass;
import java.sql.*;
import java.util.*;

public class ShowRecordByPage
{
   
    String serverName="localhost";
    String sConnStr="jdbc:oracle:thin:@localhost:1521:jh";
    String login_name="jh";
    String pwd="jh";
    Statement stmt=null;
    Connection conn=null;
    ResultSet rs=null;
    int afint;

    public ShowRecordByPage()
    {
      
        try
        {
         Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            System.err.println(classnotfoundexception.getMessage());
        }
    }


 
 public ResultSet executequery(String sql){
  try{
   conn=DriverManager.getConnection(sConnStr, login_name, pwd);
   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
   rs=stmt.executeQuery(sql);
  }catch(SQLException e){
   System.out.println("can't executeQuery");
  }
  return rs;
 }
 
 
 public int executeupdate(String sql) throws SQLException{
   try
       {
  conn=DriverManager.getConnection(sConnStr, login_name, pwd);
  stmt=conn.createStatement() ;
  afint=stmt.executeUpdate(sql);
     }catch(SQLException sqlexception)
          {
            System.err.println(sqlexception.getMessage());
              }

  return afint;
 }


public void closecon()
{
   try{
       if(rs!=null)
{
       rs.close();
}
       if(stmt!=null)
{
       stmt.close();
}
       if(conn!=null)
{
          conn.close();
}
    }catch(Exception e){}
}
}

你可能感兴趣的:(oracle,sql,jsp,mysql,jdbc)