164 DATABASE [CHAP. 8
In this case, the stored procedure is named Add_Student, and besides adding the new student, it finds
and assigns an advisor for the new student, based on the new student??™s major. All the program needs to do is set
the parameters and then execute the stored procedure. The procedure will do all the complex processing, and
then return an indication of success in the sixth parameter.
// CallableStatement object to access the stored procedure.
// Inside the curley braces, you call the procedure, and use
// question marks for parameter place-holders.
// Five are for "IN" variables to be set,
// One is for an "OUT" varible to be returned to the program
CallableStatement callStmt = con.prepareCall(
"{call Add_Student( ?, ?, ?, ?, ?, ?)}" );
// Now supply values for the parameters
// Parameters are referenced in order starting with 1.
callStmt.setString(1, "Billy Bob" );
callStmt.setString(2, "Appleby" );
callStmt.setInt (3, 1004 );
callStmt.setString(4, "585 223 4599" );
callStmt.setString(5, "Math" );
// And register the OUT variable
// This variable returns information to this program
// from the stored procedure.
callStmt.registerOutParameter( 6, java.sql.Types.VARCHAR );
// The CallableStatement object has an additional method*
// for use when the stored procedure uses multiple SQL
// statements.
Pages:
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451