jsp+oracle 的兩種分頁實現程式

來源:互聯網
上載者:User
js|oracle|程式|分頁

//*串連Oracle的Bean: 檔案名稱為:conn_oracle.java
//*--------------------------------------------------------------------------------------------------------------

package conn_oracle;
import java.sql.*;
import java.util.*;
import java.io.PrintStream;
public class conn_oracle
{

    String serverName="localhost";
    String sConnStr="jdbc:oracle:thin:@localhost:1521:oemrep";
    String login_name="scott";
    String pwd="tiger";
    Statement stmt=null;
    Connection conn=null;
    ResultSet rs=null;
    int afint;

    public conn_oracle()
    {
      
        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 的rownum 進行分頁 檔案名稱為 fy4.jsp
//*---------------------------------------------------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="DBLink" scope="page" class="conn_oracle.conn_oracle"/>

<%
//變數聲明
String mysql=new String(); //SQL語句
int intRowCount=0;  //總的記錄數
int intPageCount=0; //總的頁數
int intPageSize=5; //每頁顯示的記錄數
int intPage; //待顯示頁碼
String strPage=new String(); //用來接收當頁碼參數
int begin_no=0; //開始的rownum記錄號
int end_no=0;  //結束的rownum記錄號

//取得待顯示頁碼
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁資料
intPage = 1;
}
else{//將字串轉換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;
}


//得到總的資料記錄行數
 mysql="select count(*) total_rows from scott.performance";
   ResultSet rs=DBLink.executequery(mysql);
   if(rs.next())
 {
 intRowCount=rs.getInt("total_rows"); //這裡只能用getInt()
 //out.print("Total rows is:"+intRowCount);
  }
rs.close();

//計算總共要分多少頁
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//調整待顯示的頁碼
if(intPage>intPageCount) intPage = intPageCount;
//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>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>學號</th>
<th>姓名</th>
</tr>

<%
    begin_no=(intPage-1) * intPageSize + 1;
    end_no = intPage * intPageSize;
 //out.print("<br>begin:"+begin_no+"<br>end:"+end_no);
 mysql="select * from (select rownum row_id ,s_id,s_name from (select s_id,s_name from performance order by s_id desc)) where row_id between " +begin_no+ " and " +end_no;
    rs = DBLink.executequery(mysql);
 while(rs.next())
 {
   %>
   <tr>
       <td><%=rs.getString("s_id")%></td>
       <td><%=rs.getString("s_name")%></td>
      </tr>
   <%
      }
   rs.close();
   %>
</table>

第<%=intPage%>頁 共<%=intPageCount%>頁
<a href="fy4.jsp?page=1">首頁</a>
<%if(intPage<intPageCount){%><a href="fy4.jsp?page=<%=intPage+1%>">下一頁</a><%}%>
<%if(intPage>1){%><a href="fy4.jsp?page=<%=intPage-1%>">上一頁</a><%}%>
<a href="fy4.jsp?page=<%=intPageCount%>">尾頁</a>


<%
 //關閉資料庫連接
    DBLink.closecon();
%>

//* 一般通用的分頁方法,不過效率較低 檔案名稱為:fy2.jsy
//*------------------------------------------------------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:useBean id="DBLink" scope="page" class="conn_oracle.conn_oracle"/>

<%
//變數聲明

java.sql.ResultSet rs; //結果集對象
java.lang.String sql; //SQL語句

int intPageSize; //一頁顯示的記錄數
int intRowCount; //記錄總數
int intPageCount; //總頁數
int intPage; //待顯示頁碼
java.lang.String strPage;

int i;

//設定一頁顯示的記錄數
intPageSize = 20;

//取得待顯示頁碼
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁資料
intPage = 1;
}
else{//將字串轉換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;
}


sql = "select * from scott.performance";

//執行SQL語句並擷取結果集
rs = DBLink.executequery(sql);

//擷取記錄總數
rs.last();
intRowCount = rs.getRow();

//記算總頁數
intPageCount = (intRowCount+intPageSize-1) / intPageSize;

//調整待顯示的頁碼
if(intPage>intPageCount) intPage = intPageCount;
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP資料庫操作常式 - 資料分頁顯示 - JDBC 2.0 - Oracle</title>
</head>

<body>

<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>學號</th>
<th>姓名</th>
</tr>

<%
if(intPageCount>0){
//將記錄指標定位到待顯示頁的第一條記錄上
rs.absolute((intPage-1) * intPageSize + 1);

//顯示資料
i = 0;
while(i<intPageSize && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString("s_id")%></td>
<td><%=rs.getString("s_name")%></td>
</tr>
<%
rs.next();
i++;
}
}
%>

</table>

第<%=intPage%>頁 共<%=intPageCount%>頁
<a href="fy2.jsp?page=1">首頁</a>
<%if(intPage<intPageCount){%><a href="fy2.jsp?page=<%=intPage+1%>">下一頁</a><%}%>
<%if(intPage>1){%><a href="fy2.jsp?page=<%=intPage-1%>">上一頁</a><%}%>
<a href="fy2.jsp?page=<%=intPageCount%>">尾頁</a>

</body>
</html>

<%
//關閉結果集
rs.close();
 //關閉資料庫連接
    DBLink.closecon();
%>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.