Callable Statment

Embed Size (px)

Citation preview

  • 8/9/2019 Callable Statment

    1/10

    Callable Statement

    OADBTransactionImpl oadbtransactionimpl1 =(OADBTransactionImpl)oaapplicationmodule.getOADBTransaction(); String s1 = "begin !! oa# util p$g.c%ec$ loc #or iproc (&1'& ); end;"; Oracle allableStatement oraclecallablestatement =(Oracle allableStatement)oadbtransactionimpl1.create allableStatement(s1' 1); String s*; tr+ , oraclecallablestatement.registerOut-arameter( ' 1 ' ); oraclecallablestatement.setString(1' s); oraclecallablestatement.e ecute(); s* = oraclecallablestatement.getString( ); oraclecallablestatement.close(); /

    catc%(S0 2 ception s3le ception) , t%ro! OA2 ception.!rapper2 ception(s3le ception); /

  • 8/9/2019 Callable Statment

    2/10

    Callable Statement Cont..

    According to Oracle Standards !%ene4er it is possible !e s%ould use 5ie! Ob6ects #or Databaseoperation but in some situations !e %a4e to call - 7S0 -rocedures and 8unctions using 9DB .

    In general' to in4o$e a stored procedure78unction #rom !it%in an entit+ ob6ect or an applicationmodule' +ou need to&

    1. reate a 9DB allableStatement !it% t%e - 7S0 bloc$ containing t%e stored procedurein4ocation

    . :egister O T -arameter i# an+*. Bind an+ 4ariables.. lose t%e statement

  • 8/9/2019 Callable Statment

    3/10

    Callable Statement Cont..

    reate allable Statement&

    To create callable statement !e s%ould import 6a4a.s3l. allableStatement pac$ageI# !e are using - 7S0 -rocedure' t%en String t%at !e !ill pass to callable statement !ill be in#ollo!ing #orm&?B2@I -ac$age name.procedure name(&1'& ) 2 D; !%ere &1 and & are -arameters !%ic% ma+ beeit%er I or O T -arameters.2 &String s3lString = "begin test pac$age.prc Test(&1' & );end;"

    I# !e are using - 7S0 8unction t%en it s%ould be in #ollo!ing #orm&

    ?B2@I &1 &= -ac$age name.#unction name(& ) 2 D; !%ere &1 !ill be t%e contain t%e resultsreturned b+ database #unction and !ill be treated as O T parameter and & is I -arameter.

    2 ample&2 &?B2@I &1= Test -ac$age.#unc test(& ) 2 D;!%ere &1 !ill be considered as O T parameter and needs to be registered !%ile & is I -arameter.

  • 8/9/2019 Callable Statment

    4/10

    Callable Statement Cont..

    8ollo!ing piece o# code creates a callable atement&

    OADBTransaction t n = getDBTransaction();String s3lString = "begin dbms application in#o.set module(&1' & );end;"

    allableStatement cs = t n.create allableStatement(s3lString);

    :egister O T -arameters&Be#ore registering O T parameters !e need to import import 6a4a.s3l.T+pes; pac$ageAnd a#ter t%at !e need to use #ollo!ing statement to register O T parameter&

    ((Oracle allableStatement)cs.registerOut-arameter(1' T+pes.5A: CA:' ' );77T+pes. 2:I

    T+pes.5A: CA: can be replaced b+ ot%er datat+pes as per +our re3uirement

    Bind I -arameters&To bind in parameters !e can use #ollo!ing statement& cs.setString(1' 4alue); !%ere setString can bereplaced b+ Set umber7SetDate as per +our re3uirement.

  • 8/9/2019 Callable Statment

    5/10

    Callable Statement Cont..

    2 ecute allable Statement&To e ecute #ollo!ing statement& !e can use e ecute met%od o# callable statement.

    :etri4ing O T -arameter 5alue&!e can get t%e 4alue o# O T parameter using getString7get umber7getDate met%ods o# callablestatement.

    2 ample&messageBu##er = cs.getString(1);

    lose Statement&To close t%e statement !e can use close()met%od o# callable statement

  • 8/9/2019 Callable Statment

    6/10

    Callable Statement Cont..

    2 ample 1.

    allable Statement #or -rocedure !it% I and O T parameter.import 6a4a.s3l. allableStatement;import 6a4a.s3l.S0 2 ception;import 6a4a.s3l.T+pes;

    ...String out-aram5alue;OADBTransaction t n = getDBTransaction();

    allableStatement cs =t n.create allableStatement("begin Test -ac$age.test prc(&1' & );end;");tr+,((Oracle allableStatement)cs.registerOut-arameter( ' T+pes.5A: CA:' ' );cs.setString(1' ?Sanaari );cs.e ecute();out-aram5alue = cs.getString(1);

  • 8/9/2019 Callable Statment

    7/10

    Callable Statement Cont..

    cs.close();/catc% (S0 2 ception s3le),cs.close();

    /

  • 8/9/2019 Callable Statment

    8/10

    Callable Statement Cont..

    2 ample & alling - 7S0 8unction

    import 6a4a.s3l. allableStatement;import 6a4a.s3l.S0 2 ception;import 6a4a.s3l.T+pes;...

    String out-aram5alue;OADBTransaction t n = getDBTransaction();allableStatement cs =

    t n.create allableStatement("begin &1 &= Test -ac$age.test #unc(& );end;");tr+,((Oracle allableStatement)cs.registerOut-arameter( ' T+pes.5A: CA:' ' );cs.setString(1'Sanaari);cs.e ecute();out-aram5alue = cs.getString(1);

  • 8/9/2019 Callable Statment

    9/10

    Callable Statement Cont..

    cs.close();/catc% (S0 2 ception s3le),cs.close();

    /

  • 8/9/2019 Callable Statment

    10/10

    Callable Statement Cont..

    oapageconte t.!riteDiagnostics(t%is'"5alue o# Deli4erTo ocation is"Es'OA8!$ onstants.STAT2 2 T);

    import oracle.apps.#nd.#rame!or$.OA8!$ onstants;