Java connection Oracle Database call stored procedure Implementation paging query (EMP for example)

Source: Internet
Author: User

First step: Build a package with a cursor type

Sql>create or Replace package testpackage

As type test_cursor is REF CURSOR; --Define a cursor named Test_cursor

End Testpackage;

Step two: Write a paged stored procedure

Sql>create or replace procedure Fenye (

TableName in varchar2,--table name

PageSize in number,--records displayed per page

Pagenow in number,--current page

Myrows out number,--total records

Mypagecount out number,--total pages

P_cursor out testpackage.test_cursor--the recordset returned

) is

--Define a partial SQL statement string, and two variables

V_sql varchar (1000);

V_begin number:= (pageNow-1) *pagesize+1;

V_end Number:=pagesize*pagenow;

--Executive Section

Begin

V_sql:= ' SELECT * FROM (select A1.*,rownum rn from, select * from ' | | tablename| | ') A1 where

Rownum<= ' | | v_end| | ') where rn>= ' | | V_begin;

Open p_cursor for V_sql; --Cursor associated SQL

V_sql:= ' SELECT COUNT (*) from ' | | TableName;

Execute immediate v_sql into myrows;

If mod (myrows,pagesize) =0 Then

Mypagecount:=myrows/pagesize;

Else

mypagecount:=myrows/pagesize+1;

End If;

Close p_cursor;

End

/

Step three Java calls the process remember to introduce the JAR package

Import java.sql.*;

public class Fenye {

public static void Main (String [] args) {

try{

Class.forName ("Oracle.jdbc.driver.OracleDriver");

Connection ct=drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:orcl", "Scott", "Tiger"); ORCL-bit database name

CallableStatement Cs=ct.preparecall ("{Call Fenye (?,?,?,?,?,?)}");

To give? Assign value

Cs.setstring (1, "EMP");

Cs.setint (2,5);//Display 5 rows per page

Cs.setint (3,2);//Show Second page

Register return value

Cs.registeroutparameter (4,oracle.jdbc.oracletypes.integer);

Cs.registeroutparameter (5,oracle.jdbc.oracletypes.integer);

Cs.registeroutparameter (6,oracle.jdbc.oracletypes.cursor);

Cs.execute ();//execution

int Rownum=cs.getint (4);

int Pagecount=cs.getint (5);

ResultSet rs= (ResultSet) cs.getobject (6);

System.out.println ("RowNum" +rownum);

System.out.println ("Total pages:" +pagecount);

while (Rs.next ()) {

System.out.println ("Number:" +rs.getint (1) + "Name:" +rs.getstring (2));}

}catch (Exception e) {

E.printstacktrace ();}

}

}

Java connection Oracle Database call stored procedure Implementation paging query (EMP for example)

Related Article

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.