Java Reference
In-Depth Information
PreparedStatement objects have methods that set a designated parameter to
a given value for a particular data type, such as setString() , setBytes() , and
setBoolean() , used in lines 150 through 154. The parameters are designated
according to the order in which they appear in the prepared statement, with 1
representing the first parameter. Because no method is available to insert a
Password object into a prepared statement, a method, serializeObj(), is created
that will serialize an object into a byte array. That method is called in line 153 to
serialize the Password object, pswd. The returned byte array is passed to the
setBytes() method, setting the byte array in the PreparedStatement, pStmt, at the
fourth placeholder. After all data values have been set, line 155 executes the
PreparedStatement by calling its executeUpdate() method. Note that the same
method name for a Statement object has a string containing an SQL statement
as a parameter, but a PreparedStatement object does not, because the SQL state-
ment already has been created and passed to the DBMS. Finally, line 163 calls the
close() method, inherited from Statement, which immediately releases the
Statement object's database and JDBC resources.
The following step enters code to set the data values for and execute a
PreparedStatement.
To Execute a PreparedStatement
1. Enter lines 148 through 164 as shown in Figure 11-17 on page 711.
The String, Password, and Boolean values are set in the PreparedStatement
object, and the prepared statement is executed (Figure 11-29). The
PreparedStatement then is closed.
dynamic data
set methods
for prepared
statement
executeUpdate()
method
close() method
FIGURE 11-29
At this point, the database contains three tables: Users, Stocks, and UserStocks.
When the completed program executes, the table, Users, contains a single record
of data. This is the initial state for the database; each time this utility program
 
Search WWH ::




Custom Search