一. 使用plsql语句分页
1.sql
--假设已经存在表myTble
--分页模板
select * from (select mt.*, rownum rn from (select * from myTable) mt where rownum<=10) where rn >= 6
select * from (select mt.*, rownum rn from (select * from myTable order by id) mt where rownum<=10) where rn >= 6
最里面的select用来排序
中间的select里面用rownum进行第一次筛选
外面的select用rownum进行最后排序
里面select定义的别名可以用在外面的select里面
--定义包并在包中定义游标类型
create or replace package myPackage as type sp_cursor is ref cursor;
end ;
--编写分页存储过程
create or replace procedure pro_separatedPage
(
tableName in varchar2, --表名(输入)
pageSize in number, --每页几行(输入)
nowPage in number, --当前页(输入)
totalRows out number, --总行数(输出)
totalPages out number, --总页数(输出)
my_cursor out myPackage.sp_cursor --返回结果集(输出)
) is
v_sql varchar2(1000);
v_begin number := (nowPage-1)*pageSize+1;
v_end number := nowPage*pageSize;
begin
v_sql := 'select * from (select mt.*, rownum rn (select * from '|| tableName ||') mt where rownum<='|| v_end ||') where rn >= '|| v_begin;
--打开游标是游标和sql语句关联
open my_cursor for v_sql;
--计算总页数和总行数
v_sql := 'select count(*) from '||tableName;
execute immediate v_sql into totalRows;
if mod(totalRows,pageSize)=0 then totalPages := totalRows/pageSize;
else totalPages := totalRows/pageSize + 1;
end if;
close my_cursor;
end;
2.java:
import java.sql.*;
//测试分页
//注意添加oracle驱动,这个案例没有添加所以发生编译错误
public class SeparatedPage {
public static void main(String[] args) {
//
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.18:1521:ORCL", "scott", "scott");
CallableStatement cs = con.prepareCall("{call pro_separatedPage(?,?,?,?,?,?)}");
//给前三个?赋值并给后三个?注册
cs.setString(1, "myTable");
cs.setInt(2, 5);
cs.setInt(3, 1);
cs.registerOutParameter(4, oracle.jdbc.OracleTypes.INTEGER);
cs.registerOutParameter(5, oracle.jdbc.OracleTypes.INTEGER);
cs.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
//执行CallableStatement对象
cs.execute();
int totalRows = cs.getInt(4);//此处4这个数是由上面问号的位置决定的
int totalPages = cs.getInt(5);
ResultSet rs = (ResultSet)cs.getObject(6);
System.out.println("总行数 = "+totalRows);
System.out.println("总页数 = "+totalPages);
while(rs.next()){
System.out.println("编号 = "+rs.getInt(1));//1这个数表示myTable表中第一列的值
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
二. 使用ResultSet分页
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
* 使用resultSet分页
* 传入参数currentPage(当前第几页)和showRows(每页显示几行)两个参数
* */
public class SeparatedPage1 {
private final String FIND_PERSON_PAGE = "SELECT * FROM TABLE_NAME BY id";
private Connection getConnection() throws SQLException{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:MYORA1", "用户名", "密码");
return con;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} return null;
}
@SuppressWarnings({ "finally", "finally" })
public List<Person> getPageList(int currentPage, int showRows){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Person> personList = new ArrayList<Person>();
try {
con = getConnection();
con.prepareStatement(FIND_PERSON_PAGE);
rs = ps.executeQuery();
int skipBegin=(currentPage -1)*showRows;
int skipEnd = currentPage * showRows;
int currentNum = 0;
while(rs.next()){
if(currentNum >= skipBegin && currentNum <= skipEnd){
Person person = new Person();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
personList.add(person);
if(currentNum == skipEnd-1){//此处减1不理解
break;
}
}
currentNum ++ ;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
if(rs != null){rs.close();}
if(ps != null){rs.close();}
if(con != null){rs.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
return personList;
}
}
}
}
三. 在ResultSet中使用absolute定位分页
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
* 使用resultSet分页
* 传入参数currentPage(当前第几页)和showRows(每页显示几行)两个参数
* */
public class SeparatedPage2 {
private final String FIND_PERSON_PAGE = "SELECT * FROM TABLE_NAME BY id";
private Connection getConnection() throws SQLException{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:MYORA1", "用户名", "密码");
return con;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} return null;
}
@SuppressWarnings({ "finally", "finally" })
public List<Person> getPageList(int currentPage, int showRows){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Person> personList = new ArrayList<Person>();
try {
con = getConnection();
con.prepareStatement(FIND_PERSON_PAGE);
rs = ps.executeQuery();
int skipBegin=(currentPage -1)*showRows;
int skipEnd = currentPage * showRows;
if(rs.absolute(skipBegin) == false) //利用absolute定位
return personList;
while(rs.next()){
if(skipBegin < skipEnd){
Person person = new Person();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
personList.add(person);
if(skipBegin == skipEnd-1){//此处减1不理解
break;
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
if(rs != null){rs.close();}
if(ps != null){rs.close();}
if(con != null){rs.close();}
} catch (SQLException e) {
e.printStackTrace();
} finally{
return personList;
}
}
}
}