Oracle implements paging of stored procedures and calls stored procedures in Java code

Source: Internet
Author: User

CREATE TABLE EMP (
ID int,
ename VARCHAR2 (30),
Sale Number (7,2)--a total of 7-bit xxxxx.xx The biggest is this data
)

--Develop a package---

Create or replace package testpackage as
Type test_cursor is REF CURSOR;
End Testpackage;

--the process of beginning to write pagination
Create or replace procedure Fenye
(TableName in Varchar2,
pagesizes in number,--a page to display records
Pagenow in number,
Myrows out number,--total records
Mypagecount out number,--total pages
Recordsets returned by P_cursor out testpackage.test_cursor--
) is
--Defining variables
--Defines the SQL statement string---
V_sql VARCHAR2 (1000);--Define SQL length
--Define two integers
V_begin number:= (pageNow-1) *pagesizes+1;
V_end number:=pagenow*pagesizes;
Begin
--Start Execution-----
V_sql:= ' SELECT * FROM
(select T1.*,rownum rn from [select * from ' | | tablename| | ' ORDER by sale ' t1 where rownum<= ' | | v_end| | ')
where rn>= ' | | V_begin;
--Associating cursors with SQL----
Open p_cursor for V_sql;
--Calculate myrows and Mypagecount
--An SQL statement has been organized
V_sql:= ' SELECT COUNT (*) from ' | | TableName;
--Executes the SQL and assigns the returned value to the Myrows
Execute immediate v_sql into myrows;--the SQL statement that executes the string must be formatted
--Calculate mypagecount--
If mod (myrows,pagesizes) =0 Then
mypagecount:=myrows/pagesizes;
Else
mypagecount:=myrows/pagesizes+1;
End If;
--Close cursor
The close p_cursor;--must turn off the cursor, it is important to not be able to release the occupied space.
End

Using Java code to invoke Oracle's stored procedures

try{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Connection ct=drivermanager.getconnection ("Jdbc:oracle:thin:@192.168.1.102:1521:orcl", "Plsqltest", "plsqltest");
CallableStatement Cs=ct.preparecall ("Call Fenye (?,?,?,?,?,?)");
This is an input parameter
Cs.setstring (1, "EMP");
Cs.setint (2,3);
Cs.setint (3,1);
Two output parameters must be written on
Cs.registeroutparameter (4, Oracle.jdbc.OracleTypes.INTEGER);
Cs.registeroutparameter (5, Oracle.jdbc.OracleTypes.INTEGER);
Cs.registeroutparameter (6, Oracle.jdbc.OracleTypes.CURSOR);
Perform
Cs.execute ();
Put the return value into a Java variable
int Sumrows=cs.getint (4);
int Sumpage=cs.getint (5);
System.out.println ("Sumrows:is" +sumrows+ "Sumpage is" +sumpage);
ResultSet rs= (ResultSet) cs.getobject (6);
Shut down
while (Rs.next ())
{
System.out.println ("number =" +rs.getint (4) + "name =" +rs.getstring (2) + "salary =" +rs.getfloat (3));
}
Cs.close ();
Ct.close ();
}catch (Exception e) {
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.