jsp+oracle 的两种分页实现程序
2005-06-12 00:17
429 查看
//*连接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();
%>
//*--------------------------------------------------------------------------------------------------------------
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();
%>
相关文章推荐
- 黑马程序员_jsp+oracle分页实现
- jsp+oracle实现数据库内容以表格形式在前台显示(包含分页)
- JSP+Oracle 实现分页
- 通过JSP程序来实现的分页
- jsp+oracle 实现简单的分页
- 通过JSP程序来实现的分页
- jsp连接oracle实现分页功能的源代码
- JSP程序中response对象实现文件下载的两种方式
- Java程序操作Oracle两种方式之简单实现
- 用JSP实现的分页程序
- jsp+oracle分页实现
- 通过JSP程序来实现的分页
- 通过JSP程序来实现的分页
- jsp+oracle实现简单的分页
- ASP.NET程序中用Repeater实现分页
- Oracle实现分页语句和Sql server实现分页语句
- jsp分页显示的实现代码
- 用存储过程实现的分页程序
- 在JSP页面中实现检索数据的分页显示
- jsp中实现参数隐藏的两种方法