从 JDBC 调用 Oracle 面向对象的 PL/SQL 成员过程

发布于 2024-11-30 12:45:50 字数 1164 浏览 1 评论 0原文

在面向对象的 PL/SQL 中,我可以向类型添加成员过程和函数。这里给出了一个例子:

create type foo_type as object (
  foo number,

  member procedure proc(p in number),
  member function  func(p in number) return number
);

create type body foo_type as 
  member procedure proc(p in number) is begin
    foo := p*2;
  end proc;

  member function func(p in number) return number is begin
    return foo/p;
  end func;
end;

来自:http://www.adp-gmbh .ch/ora/plsql/oo/member.html

在 PL/SQL 中,我可以像这样调用这些成员过程/函数:

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

How can I do it with JDBC's CallableStatement?我似乎无法在文档中轻松找到这一点。

注意:这是一种可能性,内联类型构造函数:

CallableStatement call = c.prepareCall(
    " { ? = call foo_type(5).func(2) } ");

但我正在寻找的是这样的东西(使用 java.sql.SQLData 作为参数):

CallableStatement call = c.prepareCall(
    " { ? = call ?.func(2) } ");

另外、成员函数、程序都可以修改对象。如何在 Java 中取回修改后的对象?

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,

  member procedure proc(p in number),
  member function  func(p in number) return number
);

create type body foo_type as 
  member procedure proc(p in number) is begin
    foo := p*2;
  end proc;

  member function func(p in number) return number is begin
    return foo/p;
  end func;
end;

From: http://www.adp-gmbh.ch/ora/plsql/oo/member.html

In PL/SQL, I can then call these member procedures/functions like this:

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

How can I do it with JDBC's CallableStatement? I can't seem to find this in the documentation easily.

NOTE: This is one possibility, inlining the type constructor:

CallableStatement call = c.prepareCall(
    " { ? = call foo_type(5).func(2) } ");

But what I'm looking for is something like this (using java.sql.SQLData as a parameter):

CallableStatement call = c.prepareCall(
    " { ? = call ?.func(2) } ");

Also, member functions, procedures may modify the object. How can I get the modified object back in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-12-07 12:45:50

jdbc 中,您可以使用 out 变量解析和执行 PL/SQL 块。您可以准备一个可调用语句,例如:

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

然后您可以使用 CallableStatement.registerOutParameter 并在执行该语句后,使用适当的 get 函数检索该值。

你可以直接在java中直接访问FOO_TYPE类型,但是你真的想这样做吗?请参阅下面的工作示例:

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  /

这是一个测试类,用于展示如何在 SQL 对象上使用 registerOutParameter 方法,让我们调用它:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文