Use prepared execution when you need to execute the same SQL statement repeatedly. Prepared execution avoids the overhead of creating multiple SQLIStatement objects for a single statement.
The advantage of prepared execution comes when you have the same FairCom DB SQL statement executed from within a loop. Instead of creating an object during each pass through the loop, prepared execution creates an object once and only passes input parameters for each execution of the statement.
Once a stored procedure creates a SQLPStatement object, it can execute it multiple times, supplying different values for each execution.
The following example extends the previous example to use prepared execution.
Prepared Execution Example
CREATE PROCEDURE prepared_insert_customer ()
IMPORT
import java.math.*;
BEGIN
SQLPStatement p_insert_cust = new SQLPStatement (
"INSERT INTO customer VALUES (?,?) ");
int i;
String [] [] new_custs ={
{"01","ABC"},
{"02","PQR"},
{"03","XYZ"}
};
for (i=0; i<new_custs.length; i++)
{
p_insert_cust.setParam (1, Integer.valueOf (new_custs[i] [0]));
p_insert_cust.setParam (2, new_custs[i] [1]);
p_insert_cust.execute ();
}
END