Java调用PL/SQL分页存储过程的代码示例是本文我们主要要介绍的内容,我们首先给出了PL/SQL分页的存储过程的代码,然后定义SQL语句和字符串,最后介绍了Java调用的代码,接下来就让我们一起来了解一下这部分内容吧。
PL/SQL分页存储过程:
class="dp-xml">
- create or replace procedure fenye
- (tableName in varchar2, --表名
- page_size in number, --每页显示记录数
- pageNow in number, --当前页
- myrows out number, -- 总记录数
- myPageCount out number, --总页数
- my_cursor out my_new_pack.test_cursor --返回的结果集
- )is
定义SQL 语句和字符串:
- v_sql varchar2(1000);
- v_begin number := (pageNow-1)*page_size+1;
- v_end number := pageNow*page_size;
- begin
- v_sql := 'select * from (select t1.*,rownum rn from (select * from '||tableName
- ||')t1 where rownum<='||v_end||' )where rn>='||v_begin;
- open my_cursor for v_sql;
- v_sql :='select count(*) from '|| tableName;
- execute immediate v_sql into myrows;
- if mod(myrows,page_size) =0 then
- myPageCount := myrows/page_size;
- else myPageCount := myrows/page_size+1;
- end if;
- end;
JAVA调用代码:
- import java.sql.*;
- public class test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Connection conn = null;
- CallableStatement cs = null;
- ResultSet rs =null;
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:kelvin111G2","system","MANAGER");
- cs = conn.prepareCall("{call fenye(?,?,?,?,?,?)}");
- cs.setString(1, "scott.emp");
- cs.setInt(2, 5);
- cs.setInt(3, 2);
- cs.registerOutParameter(4,oracle.jdbc.OracleTypes.INTEGER);
- cs.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER);
- cs.registerOutParameter(6,oracle.jdbc.OracleTypes.CURSOR);
- cs.execute();
- System.out.println("总记录数为"+cs.getInt(4));
- System.out.println("总页数"+cs.getInt(5));
- rs = (ResultSet)cs.getObject(6);
- while(rs.next()){
- System.out.println(rs.getInt(1)+"==="+rs.getString(2)+"==="+rs.getString(3));
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try {
- rs.close();
- cs.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
关于Java调用PL/SQL分页过程的代码示例就介绍到这里了,希望本次的介绍能够对您有所帮助。