Java calls PLSQL paging Process

Source: Internet
Author: User

-- PLSQL minutes passed
Create or replace procedure fenye
(TableName in varchar2, -- table name
Page_size in number, -- displays the number of records per region
PageNow in number, -- Before Expiration
Myrows out number, -- total number of records
MyPageCount out number, -- total number
My_cursor out my_new_pack.test_cursor -- returned result set
) Is
-- Define SQL sentence strings
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 connector:

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.exe cute ();
System. out. println ("total records" + cs. getInt (4 ));
System. out. println ("Total number of records" + 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 ();
}
}
 
}

}

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.