prepStmt.setString( 1, "Jack Horner" );
prepStmt.setString( 2, "Arpers" );
prepStmt.setInt ( 3, 31 );
prepStmt.setNull ( 4, Types.VARCHAR );
// The PreparedStatement object methods:
// 1) executeUpdate ??” statements that modify the database
// 2) executeQuery ??” SELECT statements (reads)
prepStmt.executeUpdate();
Note that the PreparedStatement has different ???setter??? methods for different data types. Note, too,
that if a null value is required, the type of the null must be specified using one of the constants in the Java
Types class. The Types class has the Java database types, which may be different from the types used by the
DBMS. For instance, Oracle uses a type called VARCHAR2, but the corresponding Java type is VARCHAR.
The last interface we will discuss is the CallableStatement, which extends the PreparedStatement
interface. A CallableStatement object provides a single mechanism for a Java program to call a stored procedure
in any DBMS. A programmer sets up a CallableStatement object much like a programmer does for
a PreparedStatement object, but the syntax uses curly braces, and the stored procedure can return one or more
ResultSets as well as other parameters. We will not discuss all the possible configurations of the
CallableStatement, but here is an example of a CallableStatement that invokes a stored procedure.
Pages:
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450