In the Data interface, the updateMany() method indicates that a statement is to be run multiple times.
Although it is possible for your code to run a single DML statement repeatedly, calling the update() method with the same SQL statement and passing different parameters each time, it is usually more efficient to use the updateMany() method.
INSERT INTO HR.EMPLOYEE VALUES(:id, :lastName, :firstName, :dept, :location);You would pass values into the SQL statement by using a bean, perhaps named Employee, or a Map object. With the updateMany() method, you could use the same SQL statement. However, you would pass values by using an Iterable object of the same type as the bean or Map object.
The returned int array indicates, as in JDBC, the success (and associated update count) or failure of each run of the SQL statement. This includes use of JDBC's update counts Statement.EXECUTE_FAILED and SUCCESS_NO_INFO.
If failures occur, a com.ibm.pdq.runtime.exception.UpdateManyException exception results. This runtime exception contains information that JDBC reports in its java.sql.BatchUpdateException exception.
If you want to run a batch of SQL CALL statements, you can use the updateMany() method only if your stored procedure has no OUT or INOUT parameters and returns no query results.
Assume that Company A purchases Company B and needs to include the latter's employees in the table HRDept.Employee. The class that Company A uses to represent this table is defined as follows:
public EmplData { public String empId; public String firstName; public String middleName; public String lastName; public String deptId; public BigDecimal baseSalary; public BigDecimal bonus; public String extension; public Date hireDate; public String status; }
It is unlikely that the format of the employee records of Company B matches that used by Company A. If they did, values in the EMPNO and WORKDEPT column might conflict. So, processing is needed against Company B's records.
In this example, that processing is assumed to take place in a java.util.ArrayList<T>, where com.companyb.EmplData is the generic <T>, because it is the class used by Company B's human resources department for an employee.
After all Company B's employees have been assigned new employee IDs, and their department information is updated to reflect department IDs for Company A, the change to update the HRDept.Employee table could be made with application logic like the following:
ArrayList<EmplData> transferHires = new ArrayList<EmplData>(); EmplData transferHire1 = new EmplData (...); EmplData transferHire2 = new EmplData (...); EmplData transferHire3 = new EmplData (...); transferHires.add (transferHire1); transferHires.add (transferHire2); transferHires.add (transferHire3); Connection con = DriverManager.getConnection(...); Data db = DataFactory.getData(con); int[] manyCount = db.updateMany( "INSERT INTO HRDept.Employee(EMPNO, FIRSTNME," + " MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE) " + "VALUES(:empId, :firstName, SUBSTR(:middleName,1,1), " + ":lastName, :deptId, :extension, :hireDate)", transferHires );
This example uses this definition of the updateMany() method: public <T> int[] updateMany(java.lang.String sql, Iterable<T> parameters)