原来的分页逻辑
1、根据页数查询数据
2、查询总条数
存储过程实现就必须拿到分页数据和总条数
create package pkg_query
as
type cur_query is ref cursor;
end pkg_query;
--利用存储过程实现分页
create or replace procedure page1(
v_start in number, -- 起始条数
v_end in number, -- 结束条数
v_data out pkg_query.cur_query, -- 返回的列表
v_size out number -- 表的总条数
)is
v_sql varchar2(300);
begin
select count(*) into v_size from emp;
v_sql := 'select * from (select t.*, rownum as r from (select * from emp order by hiredate desc) t) t1 where t1.r>'||v_start||' and t1.r<='||v_end;
open v_data for v_sql;
execute immediate v_sql;
end;
jdbc调用:
public void showEmpPage() {
//加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//创建链接
Connection conn = null;
try {
conn = DriverManager.
getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
System.out.println(conn);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//创建预处理sql 对象 == 创建存错过程调用对象
try {
CallableStatement call = conn.prepareCall("call page1(?,?,?,?)");
//设置输入参数
call.setInt(1, 5);
call.setInt(2, 10);
//设置输出参数 注册第几个参数返回数据类型是什么
call.registerOutParameter(3, OracleTypes.CURSOR);
call.registerOutParameter(4, Types.INTEGER);
//发送并执行sql
call.execute();
ResultSet rs = (ResultSet) call.getObject(3);
while(rs.next()) {
System.out.println(rs.getString("ename") + " =="+rs.getDouble("sal"));
}
System.out.println(call.getObject(4));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}