close();
con.close();
There are two other interfaces that inherit from the Statement interface and provide additional
functionality. A PreparedStatement object creates a precompiled SQL command that can be executed
repeatedly with new values for the column variables. Precompilation leads to efficiency of execution.
PreparedStatements are used for repetitive tasks such as loading a table with data. For instance, the
program might repetitively (1) read a record of data to be stored, (2) set each field of the PreparedStatement
to the proper value, and (3) executeUpdate().
Here is an example of using a PreparedStatement to insert one of several new students into the database:
// A prepared statement is precompiled, and can be
// used repeatedly with new values for its parameters.
// Use question marks for parameter place-holders.
PreparedStatement prepStmt = con.prepareStatement(
"INSERT INTO Student (Sname, Dorm, Room, Phone)"
+ "VALUES ( ?, ?, ?, ? )" );
// Now supply values for the parameters using "setter" methods
// This assignment and execution would usually be within
// a loop, so that the same work could be done repeatedly for
// different values of the column variables.
// Parameters are referenced in order starting with 1.
Pages:
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449