当前位置:数据库 > SQLServer >>

java调用PLSQL分页过程

--PLSQL分頁過程
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 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,