Example of number of named parameters equals the number of arguments
The following stored procedure has five arguments
create procedure createProductDef(productname varchar(64), productdesc varchar(64), listprice float, minprice float, out prod_id float); . . . let prod_id = <value for prod_id>; end procedure;
The following Java™ code
with five parameters corresponds to the stored procedure. The question
mark characters (
?
) within the parentheses of a JDBC
call refer to the parameters. (In this case five parameters for five
arguments.) Set or register all the parameters. Name the parameters
by using the format cstmt.setString("arg", name);
,
where arg is the name of the argument in the corresponding
stored procedure. You do not need to name parameters in the same order
as the arguments in the stored procedure.String sqlCall = "{call CreateProductDef(?,?,?,?,?)}"; CallableStatement cstmt = conn.prepareCall(sqlCall); cstmt.setString("productname", name); // Set Product Name. cstmt.setString("productdesc", desc); // Set Product Description. cstmt.setFloat("listprice", listprice); // Set Product ListPrice. cstmt.setFloat("minprice", minprice); // Set Product MinPrice. // Register out parameter which should return the product is created. cstmt.registerOutParameter("prod_id", Types.FLOAT); // Execute the call. cstmt.execute(); // Get the value of the id from the OUT parameter: prod_id float id = cstmt.getFloat("prod_id");
The Java code and
the stored procedure show the following course of events:
- A call to the stored procedure is prepared.
- Parameter names indicate which arguments correspond to which parameter value or type.
- The values for the input parameters are set and the type of the output parameter is registered.
- The stored procedure executes with the input parameters as arguments.
- The stored procedure returns the value of an argument as an output parameter and the value of the output parameter is retrieved.