Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. /******************************************************************************/
  2. /*The Script used to create Object type in this example is as follows:*******/
  3. /******************************************************************************/
  4. create or replace type rectype as object(col1 varchar2(10),col2 Date,col3 Number);
  5.  
  6.  
  7.  
  8. 2)Define table of object u have defined above which u need to use in ur pl/sql procedure of some pkg.Remember define it as global table type and not inside package specification, because in that case our jdbc code would not be to find this object type.
  9. /******************************************************************************/
  10. /*The Script used to create table of Object type is as follows:*******/
  11. /******************************************************************************/
  12. create or replace type rectab as table of rectype;
  13.  
  14.  
  15. 3)Defining package specification and procedure definition
  16. /*********************************************************************************/
  17. /*The Script used to create package specification in this eg.is as follows:*******/
  18. /******************************************************************************/
  19.  
  20. create or replace package ioStructArray as
  21. procedure testproc(iorec in rectab,orec out rectab);
  22. end ioStructArray;
  23. /
  24.  
  25.  
  26.  
  27. 4)Defining package body and procedure
  28. /*********************************************************************************/
  29. /*The Script used to create package specification in this eg.is as follows:*******/
  30. /******************************************************************************/
  31. create or replace package body ioStructArray as
  32. procedure testproc(iorec in rectab,orec out rectab) is
  33. begin
  34. /*see how to loop and assign values*/
  35. for i in 1..iorec.count loop
  36. iorec(i).col1 := orec(i).col2;
  37. iorec(i).col2 := orec(i).col1;
  38. end loop;
  39. end testproc;
  40. end ioStructArray;
  41. /
  42.  
  43. 5)Getting connection object in JDBC Code :
  44. //Getting db connection in a jdbc
  45. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  46. // Connect to the database
  47. Connection conn=DriverManager.getConnection ("jdbc:oracle:oci8:@S692815.WORLD",
  48. "scott", "tiger");
  49.  
  50.  
  51.  
  52. 6)If writing in AM in OAF page, code to get connection:
  53. OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)getOADBTransaction();
  54. OracleConnection conn = (OracleConnection)oadbtransactionimpl.getJdbcConnection();
  55.  
  56. 7)Lets, consider a simple scenario where we have a XXVO in AM and I have to pass all VO rows data to the pl/sql procedure we just created and then receieve a table of data back and then based on it values do something in AM.Here is the Code:
  57. //imports
  58. import oracle.sql.*;
  59. import oracle.jdbc.driver.OracleConnection;
  60. import oracle.jdbc.driver.OracleCallableStatement;
  61. import oracle.apps.fnd.framework.server.OADBTransaction;
  62. import oracle.apps.fnd.framework.server.OADBTransactionImpl;
  63.  
  64. Public void arryToPLSQL()
  65. {
  66. //Getting Db connection
  67. OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)getOADBTransaction();
  68. OracleConnection conn = (OracleConnection)oadbtransactionimpl.getJdbcConnection();
  69.  
  70. //Defining variables
  71.  
  72. //oracle.sql.ARRAY we will use as out parameter from the package
  73. //and will store pl/sql table
  74. ARRAY message_display = null;
  75.  
  76. //ArrayList to store object of type struct
  77. ArrayList arow= new ArrayList();
  78.  
  79. //StructDescriptor >> use to describe pl/sql object
  80. //type in java.
  81. StructDescriptor voRowStruct = null;
  82.  
  83. //ArrayDescriptor >> Use to describe pl/sql table
  84. //as Array of objects in java
  85. ArrayDescriptor arrydesc = null;
  86.  
  87. //Input array to pl/sql procedure
  88. ARRAY p_message_list = null;
  89.  
  90. //Oracle callable statement used to execute procedure
  91. OracleCallableStatement cStmt=null;
  92.  
  93. try
  94. {
  95. //initializing object types in java.
  96. voRowStruct = StructDescriptor.createDescriptor("RECTYPE",conn);
  97. arrydesc = ArrayDescriptor.createDescriptor("RECTAB",conn);
  98. }
  99.  
  100. catch (Exception e)
  101. {
  102. throw OAException.wrapperException(e);
  103. }
  104.  
  105. for(XXVORowImpl row = (XXVORowImpl)XXVO.first();
  106. row!=null;
  107. row = (XXVORowImpl)XXVO.next())
  108. {
  109. //We have made this method to create struct arraylist
  110. // from which we will make ARRAY
  111. //the reason being in java ARRAY length cannot be dynamic
  112. //see the method defination below.
  113. populateObjectArraylist(row,voRowStruct,arow);
  114. }
  115.  
  116. //make array from arraylist
  117. STRUCT [] obRows= new STRUCT[arow.size()];
  118. for(int i=0;i < arow.size();i++)
  119. {
  120. obRows[i]=(STRUCT)arow.get(i);
  121. }
  122.  
  123. try
  124. {
  125. p_message_list = new ARRAY(arrydesc,conn,obRows);
  126. }
  127. catch (Exception e)
  128. {
  129. throw OAException.wrapperException(e);
  130. }
  131.  
  132. //jdbc code to execute pl/sql procedure
  133. try
  134. {
  135. cStmt
  136. =(OracleCallableStatement)conn.prepareCall("{CALL ioStructArray.testproc(:1,:2)}");
  137. cStmt.setArray(1,p_message_list);
  138. cStmt.registerOutParameter(2,OracleTypes.ARRAY,"RECTAB");
  139. cStmt.execute();
  140.  
  141. //getting Array back
  142. message_display = cStmt.getARRAY(2);
  143. //Getting sql data types in oracle.sql.datum array
  144. //which will typecast the object types
  145. Datum[] arrMessage = message_display.getOracleArray();
  146.  
  147. //getting data and printing it
  148. for (int i = 0; i < arrMessage.length; i++)
  149. {
  150. oracle.sql.STRUCT os = (oracle.sql.STRUCT)arrMessage[i];
  151. Object[] a = os.getAttributes();
  152. System.out.println("a [0 ] >>attribute1=" + a[0]);
  153. System.out.println("a [1 ] >>attribute2=" + a[1]);
  154. System.out.println("a [2 ] >>attribute3=" + a[2]);
  155. //You can typecast back these objects to java object type
  156.  
  157.  
  158. }
  159.  
  160. }
  161. catch (Exception e1)
  162. {
  163. throw OAException.wrapperException(e1);
  164. }
  165. }
  166.  
  167.  
  168.  
  169. /*Our custom method which will populate
  170. arraylist with struct object type
  171. */
  172. public void populateObjectArraylist( XXVORowImpl row,StructDescriptor voRowStruct , ArrayList arow)
  173. {
  174. Object[] attribMessage = new Object[3];
  175. String attr1 = null;
  176. Date attr2 = null;
  177. Number attr3 = null;
  178.  
  179. //Get value from Vo row and put in attr1,att2 and attr 3
  180.  
  181. //Putting values in object array
  182. attribMessage[0]=attr1;
  183. attribMessage[1]=attr2;
  184. attribMessage[2]=attr3;
  185.  
  186. try
  187. {
  188. STRUCT loadedStructTime = new STRUCT(voRowStruct, conn, attribMessage);
  189. arow.add(loadedStructTime);
  190. }
  191. catch (Exception e)
  192. {
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement