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?
In
jdbc
you can parse and execute PL/SQL blocks without
variables. You could prepare a callable statement such as: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:This is a test class to show how you can use the method
registerOutParameter
on an SQL object, let's call it: