At times, you need to send big list of data to backend. The backend procedure written by you or your database developed allows it to take an Oracle Type, which is a convenient approach for some scenarios.
So, this article is a basic example to perform the above using combination of Oracle PL/SQL and Java.
Using below steps we can pass an array form java and iterate it in a proc/function (I am assuming, once we are able to iterate the array, we should be able to insert as well )
Create a Oracle Type using table of VARCHAR, this can used as a placeholder for array in proc/function.
CREATE OR REPLACE TYPE MY_SCHEMA.txn_type AS TABLE OF varchar (500);
Create a proc/function to iterate and insert data.
CREATE OR REPLACE PROCEDURE CCIEADM.INSERT_CONFIG ( P_ARRAY IN txn_type, STATUS OUT VARCHAR2 ) AS V_COUNT NUMBER; BEGIN V_COUNT := 0; FOR i IN 1 .. P_ARRAY.COUNT LOOP DBMS_OUTPUT.put_line ( 'At position:' ||v_count); DBMS_OUTPUT.put_line ('Value:' || P_ARRAY (i) ); V_COUNT := V_COUNT+1; END LOOP; END; /
Oracle code to call this function, can help testing your procedure
DECLARE P_ARRAY KK_TXN_TYPE; STATUS VARCHAR2(32767); BEGIN P_ARRAY := P_ARRAY := TXN_TYPE('sale', 'authenticate', 'create_token') ;
-- Modify the code to initialize this parameter STATUS := 1; MY_SCHEMA.INSERT_CONFIG ( P_ARRAY, STATUS ); COMMIT; END;
A java code snippet which takes array as input and call the above procedure.
public String insertTransactions(String[] transactionsTypes) { Connection myConnection = null; CallableStatement cstmt = null; String result = null; ResultSet oResultSet = null; try { myConnection = ConnectionFactory.getConnection("connPool"); // ConnectionFactory is a util class to get connection ArrayDescriptor arrayDiscriptor = null; arrayDiscriptor = ArrayDescriptor.createDescriptor( "MY_SCHEMA.TXN_TYPE", myConnection);
// first argument is the oracle sqltype cstmt = myConnection.prepareCall("call MY_SCHEMA.INSERT_CONFIG(?)"); Array array = new ARRAY(arrayDiscriptor, myConnection, transactionsTypes); //connection passed here should be compatible to the OracleConnection if (array == null) { System.out.println("array does not exist"); } else { System.out.println("yo!! Array created!!"); } cstmt.setArray(1, array); cstmt.registerOutParameter(3, OracleTypes.VARCHAR); cstmt.execute(); result = cstmt.getString(2); System.out.println("proc result in merchant db is " + result); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (oResultSet != null) { oResultSet.close(); } if (cstmt != null) { cstmt.close(); } if (myConnection != null) { myConnection.close(); } } catch (SQLException e) { e.printStackTrace(); } } return result; } public static void main(String[] s) { String transactionTypes[] = { "sale", "create_token", "authentication" }; String status = new TransactionDAO().insertTransactions(reqFieldsValue); System.out.println("Status" + status); }