Java Reference
In-Depth Information
Modifying Records with an SQL Update
Of the three tables in this database, only the Users table can be updated.
UserStocks either has a record for a given user and stock combination or it does
not, so records are only added or deleted, not updated. Although the Stocks table
could be updated, the only field is the stock name. The stock name field is
entered when a stock initially is added to the database and is not expected to
change; therefore, no provision currently exists for updating Stock table records.
The SQL UPDATE statement is used to update field values in a given record.
The SET clause indicates a comma-
delimited list of fields and their new values.
This list does not have to be in the order
created in the table, nor does every field
have to be assigned a new value. If a field is
not listed with a new value, it retains its
value prior to the update. The update is
applied to all records matching the search
condition following the WHERE keyword.
The following step enters code to
update a record in the Users table.
Using a WHERE clause in an UPDATE
Typically, the search condition specified in a
WHERE clause of an SQL UPDATE statement
references the primary key, which is unique for
each record. If the search condition specified
criteria other than a primary key, then the
update is applied to all matching records,
which may be more than one record.
To Update a Record in the Users Table
1. Enter lines 146 through 189 as shown in Figure 11-36.
TextPad displays the updUser() method (Figure 11-44). Much like the
addUser() method, this method requires a User object as a parameter and
sets a serialized Password as a dynamic parameter to a PreparedStatement.
PreparedStatement
User object
updUser()
method
object
serialized
FIGURE 11-44
 
Search WWH ::




Custom Search