Sample Code for returning a result set using a cursor in an Oracle Stored Procedure

Source: Internet
Author: User

Oracle Stored ProcedureYesReturns a result set using a cursor.Its implementation method is what we will introduce in this article. Next we will introduce it. First, we create the following content in sqlplus:

1. Package

 
 
  1. SQL> create or replace package types
  2. 2
  3. 3 type cursorType is ref cursor;
  4. 4 end;
  5. 5/
  6. The package has been created.

2. Functions

 
 
  1. SQL> create or replace function sp_ListEmp return types. cursortype
  2. 2
  3. 3 l_cursor types. cursorType;
  4. 4 begin
  5. 5 open l_cursor for select id, title from cf_news order by id; -- table name
  6. 6 return l_cursor;
  7. 7 end;
  8. 8/
  9. The function has been created.

3. Stored Procedure

 
 
  1. SQL> create or replace procedure getemps (p_cursor in out types. cursorType)
  2. 2
  3. 3 begin
  4. 4 open p_cursor for select id, title from cf_news order by id; -- table name
  5. 5 end;
  6. 6/
  7. The process has been created.

4. Create an executable java console Program

 
 
  1. Import java. SQL .*;
  2. Import java. io .*;
  3. Import oracle. jdbc. driver .*;
  4. Class GetValues
  5. Public static void main (String args [])
  6. Throws SQLException, ClassNotFoundException
  7. {
  8. String driver_class = "oracle. jdbc. driver. OracleDriver ";
  9. String connect_string = "jdbc: oracle: thin: @ 127.0.0.1: 1521: database ";
  10. String query = "begin: 1: = sp_listEmp; end;"; // call the previously created function here!
  11. Connection conn;
  12. Class. forName (driver_class );
  13. Conn = DriverManager. getConnection (connect_string, "scott", "tiger ");
  14. CallableStatement cstmt = conn. prepareCall (query );
  15. Cstmt. registerOutParameter (1, OracleTypes. CURSOR );
  16. Cstmt.exe cute ();
  17. ResultSet rset = (ResultSet) cstmt. getObject (1 );
  18. While (rset. next ())
  19. System. out. println (rset. getString (1 ));
  20. Cstmt. close ();
  21. }

Run the above Code in the Java compiler to implement this function.

The above is the whole process of returning the result set using the cursor in the Oracle stored procedure. This article will introduce it here. I hope this introduction will be helpful to you!

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.