Call Oracle object-oriented PL/SQL member procedures from JDBC

前端 未结 1 1805
我寻月下人不归
我寻月下人不归 2021-01-05 05:24

In object-oriented PL/SQL, I can add member procedures and functions to types. An example is given here:

create type foo_type as object (
  foo number,

  me         


        
相关标签:
1条回答
  • 2021-01-05 06:07

    In jdbc you can parse and execute PL/SQL blocks with out variables. You could prepare a callable statement such as:

    declare
        x foo_type;
    begin
        x := foo_type(5);
        x.proc(10);
        ? := x.func(2);
    end;
    

    Then you can use CallableStatement.registerOutParameter and after the statement has been executed, use the appropriate get function to retrieve the value.

    You can access directly a FOO_TYPE type directly in java, but do you really want to do this? See below for a working example:

    SQL> create or replace and compile java source named "TestOutParam" as
      2  import java.sql.*;
      3  import oracle.sql.*;
      4  import oracle.jdbc.driver.*;
      5  
      6  public class TestOutParam {
      7  
      8     public static int get() throws SQLException {
      9  
     10        Connection conn =
     11           new OracleDriver().defaultConnection();
     12  
     13        StructDescriptor itemDescriptor =
     14           StructDescriptor.createDescriptor("FOO_TYPE",conn);
     15  
     16        OracleCallableStatement call =
     17           (OracleCallableStatement) conn.prepareCall("declare\n"
     18              + "    x foo_type;\n"
     19              + "begin\n"
     20              + "    x := foo_type(5);\n"
     21              + "    x.proc(10);\n"
     22              + "    ? := x;\n"
     23              + "end;\n");
     24  
     25        call.registerOutParameter(1, OracleTypes.STRUCT, "FOO_TYPE");
     26  
     27        call.execute();
     28  
     29        STRUCT myObj = call.getSTRUCT(1);
     30  
     31        Datum[] myData = myObj.getOracleAttributes();
     32  
     33        return myData[0].intValue();
     34  
     35     }
     36  }
     37  /
    

    This is a test class to show how you can use the method registerOutParameter on an SQL object, let's call it:

    SQL> CREATE OR REPLACE
      2  FUNCTION show_TestOutParam RETURN NUMBER
      3  AS LANGUAGE JAVA
      4  NAME 'TestOutParam.get() return java.lang.int';
      5  /
    
    Function created
    
    SQL> select show_testoutparam from dual;
    
    SHOW_TESTOUTPARAM
    -----------------
                   20
    
    0 讨论(0)
提交回复
热议问题