How to call the returned list of Oracle stored procedures in java

Source: Internet
Author: User

The following articles mainly show you how to use java to call the return list of Oracle stored procedures so that there is no return value in Oracle stored procedures. All the return values of Oracle stored procedures are replaced by relevant parameters, if you are interested in the actual operations on the list returned by calling the Oracle stored procedure in java, you can browse our article.

The list is no exception, but because it is a set, you cannot use a common parameter. You must use pagkage. Therefore, the list must be divided into two parts,

1. Create a package. As follows:

CREATE OR REPLACE PACKAGE TESTPACKAGE
TYPE Test_CURSOR is ref cursor;
End TESTPACKAGE;

2. Create a stored procedure:

Create or replace procedure testc (p_CURSOR out TESTPACKAGE. Test_CURSOR) IS
BEGIN
OPEN p_CURSOR for select * from hyq. TESTTB;
End testc;

As you can see, Oracle storage regards the cursor as a pointer) and returns the value as an out parameter. Use the following code when calling in java:

 
 
  1. package com.hyq.src;  
  2. import java.sql.*;  
  3. import java.io.OutputStream;  
  4. import java.io.Writer;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import oracle.jdbc.driver.*;  
  8. public class TestProcedureTHREE {  
  9. public TestProcedureTHREE() {  
  10. }  
  11. public static void main(String[] args ){  
  12. String driver = "oracle.jdbc.driver.OracleDriver";  
  13. String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";  
  14. Statement stmt = null;  
  15. ResultSet rs = null;  
  16. Connection conn = null;  
  17. try {  
  18. Class.forName(driver);  
  19. conn = DriverManager.getConnection(strUrl, "hyq", "hyq");  
  20. CallableStatement proc = null;  
  21. proc = conn.prepareCall("{ call hyq.testc(?) }");  
  22. proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);  
  23. proc.execute();  
  24. rs = (ResultSet)proc.getObject(1);  
  25. while(rs.next())  
  26. {  
  27. System.out.println("<tr><td>" + rs.getString(1)
     + "</td><td>"+rs.getString(2)+"</td></tr>");  
  28. }  
  29. }  
  30. catch (SQLException ex2) {  
  31. ex2.printStackTrace();  
  32. }  
  33. catch (Exception ex2) {  
  34. ex2.printStackTrace();  
  35. }  
  36. finally{  
  37. try {  
  38. if(rs != null){  
  39. rs.close();  
  40. if(stmt!=null){  
  41. stmt.close();  
  42. }  
  43. if(conn!=null){  
  44. conn.close();  
  45. }  
  46. }  
  47. }  
  48. catch (SQLException ex1) {  
  49. }  
  50. }  
  51. }  
  52. }   

Note that the oracle driver package must be placed in the class path before execution. Otherwise, an error will be reported. the above is an introduction to the list of returned results of calling the Oracle stored procedure in java. I hope you will get some benefits.
 

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.