简体   繁体   中英

how I can copy the package source from database to my local machine using jdbc?

I want to copy all the packages's source in my local machine using jdbc.

select DBMS_METADATA.GET_DDL('PACKAGE_BODY','COLLECTSTATS','MYSCHEMA') from DUAL;

I have tried this but not getting it

You can create a custom table with ddl for each objects (packages, procedures, functions) like the following:

drop table myobjects;
create table myobjects (obj_name varchar2(128), sub_obj_name varchar2(128), obj_type varchar2(128), obj_ddl clob);
set serveroutput on
declare
   uobj_ddl clob;
   cnt number := 1;
begin

   for dt in (select object_name, subobject_name, object_type from user_objects where object_type IN ('FUNCTION','PROCEDURE','PACKAGE')) loop
    --dbms_output.put_line(dt.object_name);
    uobj_ddl := dbms_metadata.get_ddl(upper(dt.object_type), upper(dt.object_name));
    insert into myobjects values (dt.object_name, dt.subobject_name, dt.object_type, uobj_ddl);
    if mod(cnt,100) = 0 then
       commit;
    end if;
    cnt := cnt + 1;
end loop;
commit;
end;
/

Then you can do the select on this from java using jdbc:

select * from myobjects;

Sample java code:

try {
    pStat = con.prepareStatement("select * from myobjects";
    ResultSet rSet = pStat.executeQuery();
    while(rSet.next()){
        objName = rSet.getString(1);
        subObjName = rSet.getString(2);
        objType = rSet.getString(3);
        objDDL = rSet.getClob(4);
    }
    rSet = null;
} catch (SQLException e) {
    e.printStackTrace();
}
pStat = null;

The issue is probably with output size. The code of a procedure or package is bigger that 32k which is the varchar2 limitation in PL/SQL.

Even the sqlplus buffer may fail with huge packages.

One solution can be using the following query in a cursor: select text from user_source where name = 'COLLECTSTATS' order by line asc;

This will work for packages, functions, procedures and triggers. Views are managed differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM