Java code example for calling the PL/SQL paging Stored Procedure

Source: Internet
Author: User

JavaCallPL/SQL paging Stored ProcedureThe sample code is the content we will introduce in this article. We first give the code of the PL/SQL paging stored procedure, and then define the SQL statements and strings, finally, I introduced the Java calling code. Next let's take a look at this part.

PL/SQL paging stored procedure:

 
 
  1. Create or replace procedure fenye
  2. (TableName in varchar2, -- table name
  3. Page_size in number, -- number of records displayed per page
  4. PageNow in number, -- current page
  5. Myrows out number, -- total number of records
  6. MyPageCount out number, -- total number of pages
  7. My_cursor out my_new_pack.test_cursor -- returned result set
  8. ) Is

Define SQL statements and strings:

 
 
  1. v_sql varchar2(1000);  
  2. v_begin number := (pageNow-1)*page_size+1;  
  3. v_end number := pageNow*page_size;  
  4. begin  
  5. v_sql :=  'select * from (select t1.*,rownum rn from (select * from '||tableName  
  6. ||')t1 where rownum<='||v_end||' )where rn>='||v_begin;  
  7. open my_cursor for v_sql;  
  8. v_sql :='select count(*) from '|| tableName;  
  9. execute immediate v_sql into myrows;  
  10. if mod(myrows,page_size) =0 then   
  11. myPageCount := myrows/page_size;  
  12. else myPageCount := myrows/page_size+1;  
  13. end if;  
  14. end; 

JAVA call code:

 
 
  1. Import java. SQL .*;
  2. Public class test {
  3. /**
  4. * @ Param args
  5. */
  6. Public static void main (String [] args ){
  7. // TODO Auto-generated method stub
  8. Connection conn = null;
  9. CallableStatement cs = null;
  10. ResultSet rs = null;
  11. Try {
  12. Class. forName ("oracle. jdbc. driver. OracleDriver ");
  13. Conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: kelvin111G2", "system", "MANAGER ");
  14. Cs = conn. prepareCall ("{call fenye (?,?,?,?,?,?)} ");
  15. Cs. setString (1, "scott. emp ");
  16. Cs. setInt (2, 5 );
  17. Cs. setInt (3, 2 );
  18. Cs. registerOutParameter (4, oracle. jdbc. OracleTypes. INTEGER );
  19. Cs. registerOutParameter (5, oracle. jdbc. OracleTypes. INTEGER );
  20. Cs. registerOutParameter (6, oracle. jdbc. OracleTypes. CURSOR );
  21. Cs.exe cute ();
  22. System. out. println ("Total number of records" + cs. getInt (4 ));
  23. System. out. println ("total page number" + cs. getInt (5 ));
  24. Rs = (ResultSet) cs. getObject (6 );
  25. While (rs. next ()){
  26. System. out. println (rs. getInt (1) + "=" + rs. getString (2) + "=" + rs. getString (3 ));
  27. }
  28. } Catch (Exception e ){
  29. E. printStackTrace ();
  30. } Finally {
  31. Try {
  32. Rs. close ();
  33. Cs. close ();
  34. Conn. close ();
  35. } Catch (SQLException e ){
  36. // TODO Auto-generated catch block
  37. E. printStackTrace ();
  38. }
  39. }
  40. }
  41. }

The code example for calling the PL/SQL paging process in Java is described here. I hope this introduction will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.