jsp+oracle 的两种分页实现程序

80酷酷网    80kuku.com

  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("
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("
begin:"+begin_no+"
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();
%>



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: