Stored Procedure in Java call package (paging)

Source: Internet
Author: User

The stored procedure on this page is transferred from the Internet:

Http://kingmxj.javaeye.com/blog/438267

 

 

Create or replace package package_page
Type cursor_page is ref cursor;
Procedure proc_page (
P_tablename varchar2, -- table name EMP E
P_tablecolumn varchar2, -- Query column E. ID, E. ename, E. Job
P_order varchar2, -- Sort E. ename DESC
P_pagesize number, -- size of each page
P_curpage number, -- current page
P_where varchar2, -- Query condition E. ename like '% S %'
P_rowcount out number, -- total number of items, output parameter
P_pagecount out number, -- total number of pages
P_cursor out cursor_page); -- result set
End package_page;
/

Create or replace package body package_page
Is
-- Stored Procedure
Procedure proc_page (
P_tablename varchar2, -- table name EMP E
P_tablecolumn varchar2, -- Query column E. ID, E. ename, E. Job
P_order varchar2, -- Sort E. ename DESC
P_pagesize number, -- size of each page
P_curpage number, -- current page
P_where varchar2, -- Query condition E. ename like '% S %'
P_rowcount out number, -- total number of items, output parameter
P_pagecount out number, -- total number of pages
P_cursor out cursor_page -- result set
)
Is
V_count_ SQL varchar2 (2000 );
V_select_ SQL varchar2 (2000 );
Begin
-- Query the total number of items
V_count_ SQL: = 'select count (*) from' | p_tablename;
-- Connection query condition (''also belongs to is null)
If p_where is not null then
V_count_ SQL: = v_count_ SQL | 'where' | p_where;
End if;
-- Execute the query to query the total number of items
Execute immediate v_count_ SQL into p_rowcount;

-- Dbms_output.put_line ('Total number of queries SQL => '| v_count_ SQL );
-- Dbms_output.put_line ('Total number of queries COUNT = '| p_rowcount );

-- Get the total number of pages
If Mod (p_rowcount, p_pagesize) = 0 then
P_pagecount: = p_rowcount/p_pagesize;
Else
P_pagecount: = p_rowcount/p_pagesize + 1;
End if;

-- If the query record is greater than 0, the query result set is displayed.
If p_rowcount> 0 and p_curpage> = 1 and p_curpage <= p_pagecount then

-- Query all (only one page)
If p_rowcount <= p_pagesize then
V_select_ SQL: = 'select' | p_tablecolumn | 'from' | p_tablename;
If p_where is not null then
V_select_ SQL: = v_select_ SQL | 'where' | p_where;
End if;
If p_order is not null then
V_select_ SQL: = v_select_ SQL | 'ORDER BY' | p_order;
End if;
Elsif p_curpage = 1 then -- query the first page
V_select_ SQL: = 'select' | p_tablecolumn | 'from' | p_tablename;
If p_where is not null then
V_select_ SQL: = v_select_ SQL | 'where' | p_where | 'and rownum <=' | p_pagesize;

Else
V_select_ SQL: = v_select_ SQL | 'where rownum <= '| p_pagesize;
End if;
If p_order is not null then
V_select_ SQL: = v_select_ SQL | 'ORDER BY' | p_order;
End if;
Else -- query a specified page
V_select_ SQL: = 'select * from (select '| p_tablename |'. '| p_tablecolumn |', rownum row_num from '| p_tablename;

If p_where is not null then
V_select_ SQL: = v_select_ SQL | 'where' | p_where;
End if;
If p_order is not null then
V_select_ SQL: = v_select_ SQL | 'ORDER BY' | p_order;
End if;
V_select_ SQL: = v_select_ SQL | ') Where row_num>' | (p_curpage-1) * p_pagesize | 'and row_num <=' | (p_curpage * p_pagesize );

End if;
-- Execute Query
-- Dbms_output.put_line ('query Statement => '| v_select_ SQL );
Open p_cursor for v_select_ SQL;
Else
-- Dbms_output.put_line ('query Statement => '| 'select * from' | p_tablename | 'where 1! = 1 ');

Open p_cursor for 'select * from' | p_tablename | 'where 1! = 1 ';
End if;

End proc_page;
End package_page;
/

 

Import Java. SQL. callablestatement; <br/> Import Java. SQL. connection; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. util. arraylist; <br/> Import Java. util. date; <br/> Import Java. util. list; </P> <p> Import oracle. JDBC. driver. oracletypes; </P> <p> Import COM. cl. dao. bean. mcBean; <br/> Import COM. cl. dao. bean. pagemcbean; <br/> Import COM. cl. dao. DB. mydatasource; <br/> Import com. Cl. dao. INF. mcdao; </P> <p>/** <br/> * @ Param tablename <br/> * 1. table name 2. find all *, <br/> * @ Param orderby <br/> * 3. sorting field <br/> * @ Param onecount <br/> * 4. number of pages per page, <br/> * @ Param nowpage <br/> * 5. current page <br/> * @ Param constraint <br/> * 6. search Condition <br/> * 7. total number of returned entries, 8. total number of pages, 9. and a cursor <br> <br/> * For example, package_page.proc_page ('t _ Mc ',' * ', 'nid DESC', 5, 2, '1 = 1 <br/> * ', v_rowcount, v_pagecount, v_cursor) <br> <br/> */< Br/> Public pagemcbean searchmc (string tablename, string orderby, int onecount, <br/> int nowpage, string constraint) {<br/> pagemcbean = new pagemcbean (); <br/> connection conn = mydatasource. getinstance (). getconnection (); <br/> callablestatement cstmt = NULL; <br/> resultset rs = NULL; <br/> try {<br/> cstmt = conn <br/>. preparecall ("{call package_page.proc_page (?,?,?,?,?,?,?,?,?)} "); <Br/> cstmt. setstring (1, tablename); <br/> cstmt. setstring (2, "*"); <br/> cstmt. setstring (3, orderby); <br/> cstmt. setint (4, onecount); <br/> cstmt. setint (5, nowpage); <br/> cstmt. setstring (6, constraint); </P> <p> cstmt. registeroutparameter (7, oracletypes. integer); <br/> cstmt. registeroutparameter (8, oracletypes. integer); <br/> cstmt. registeroutparameter (9, oracletypes. cursor); <br/> cstmt.exe cute (); </P> <P> int rowcount = cstmt. getint (7); <br/> int pagecount = cstmt. getint (8); <br/> rs = (resultset) cstmt. getObject (9); <br/> int Cols = Rs. getmetadata (). getcolumncount (); <br/> List <McBean> List = new arraylist <McBean> (); <br/> while (RS. next () {<br/> int nid = Rs. getint ("NID"); <br/> string sname = Rs. getstring ("sname"); <br/> string sdescription = Rs. getstring ("sdescription"); // product description <br/> double nprice = Rs. getdouble ("nprice"); <br/> string simg = Rs. getstring ("simg"); <br/> string smctag = Rs. getstring ("smctag"); // whether loan is missing <br/> date dcdate = new date (RS. getdate ("dcdate "). gettime (); // Add time <br/> int nmaxid = Rs. getint ("nmaxid"); <br/> int nminid = Rs. getint ("nminid"); </P> <p> McBean = new McBean (); <br/> McBean. setnid (NID); <br/> McBean. setsname (sname); <br/> McBean. setsdescription (sdescription); <Br/> McBean. setnprice (nprice); <br/> McBean. setsimg (simg); <br/> McBean. setsmctag (smctag); <br/> McBean. setdcdate (dcdate); <br/> McBean. setnmaxid (nmaxid); <br/> McBean. setnminid (nminid); </P> <p> list. add (McBean); <br/>}< br/> pagemcbean. setlist (list); <br/> pagemcbean. setpagecount (pagecount); <br/> pagemcbean. setrowcount (rowcount); </P> <p> return pagemcbean; <br/>} catch (sqlexception e) {<br/> E. printstacktr ACE (); <br/>}finally {<br/> If (RS! = NULL) {<br/> try {<br/> Rs. close (); <br/>}catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}< br/> If (cstmt! = NULL) {<br/> try {<br/> cstmt. close (); <br/>}catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}< br/> If (Conn! = NULL) {<br/> try {<br/> Conn. close (); <br/>}catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return NULL; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> pagemcbean = new mcdaoimpl (). searchmc ("t_mc", "nid asc", 5, 3, ""); <br/> List <McBean> List = pagemcbean. getlist (); <br/> for (McBean Bean: List) {<br/> system. out. println (bean); <br/>}< br/> system. out. println (pagemcbean. getpagecount (); <br/> system. out. println (pagemcbean. getrowcount (); <br/>}

 

 

The class in the ojdbc14.jar package must be used here: Oracle. JDBC. Driver. oracletypes;

The stored procedure can be used in a universal manner, and the bean corresponding to the displayed data can be defined as needed.

 

 

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.