如何通过jdbc调用嵌套过程

时间:2013-07-25 08:19:00

标签: oracle jdbc oracle10g

请告诉我如何使用可调用语句通过JDBC调用嵌套过程。我想将(1个字符串和1个整数)存储到在运行时创建的表中。

create or replace procedure mytable (
Ptable_name in varchar2
, Pemp_name in varchar2
, Pemp_age in number
 ) is

 procedure myvalues (
   Pemp_name in varchar2
 , Pemp_age in number
   ) is
begin
 execute immediate 'insert into ' || Ptable_name
                || ' values (:emp_name, :emp_age)'
      using Pemp_name, Pemp_age;
end;

begin
execute immediate 'create table ' || Ptable_name
                  || ' (sname varchar2(20), sage number (4))';

myvalues ( Pemp_name, Pemp_age);
end;

1 个答案:

答案 0 :(得分:0)

工作演示:

SQL> create or replace procedure mytable (
Ptable_name in varchar2
, Pemp_name in varchar2
, Pemp_age in number
 ) is

 procedure myvalues (
   Pemp_name in varchar2
 , Pemp_age in number
   ) is
begin
 execute immediate 'insert into ' || Ptable_name
                || ' values (:emp_name, :emp_age)'
      using Pemp_name, Pemp_age;
end;

begin
execute immediate 'create table ' || Ptable_name
                  || ' (sname varchar2(20), sage number (4))';

myvalues ( Pemp_name, Pemp_age);
end;
/  

Procedure created.

SQL> select table_name from user_tables
where table_name = 'PTABLE';   

no rows selected

Java程序

    Connection con;
    OracleDataSource ods = new OracleDataSource();
    ods.setURL("jdbc:oracle:thin:c##test/test@192.168.1.200:1521:orcl");
    con=ods.getConnection(); 
    CallableStatement cs =con.prepareCall("begin mytable( ?, ?, ? ); end;");
    cs.setString(1, "ptable");
    cs.setString(2, "pname");
    cs.setInt(3, 1);
    cs.executeUpdate();
    cs.close();
    con.close();

和结果:

SQL> select table_name from user_tables
where table_name = 'PTABLE';   

TABLE_NAME
-------------
PTABLE

SQL> insert into ptable values( 'a', 22 );

1 row created.
SQL> select * from ptable;

SNAME                SAGE
-------------------- ----------
a                    22
pname                 1