Java Reference
In-Depth Information
If a future application needs to update a different table in this database, the
StockTrackerDB class easily can be modified to include the appropriate method
without affecting other applications using the current code.
Performing an SQL Delete
Just as all three tables required methods to add records, consequently all
three require methods to delete records. According to the requirements docu-
ment, any user can delete a stock from their holdings, but not directly from the
Stocks table. A public method is needed to delete a specific user stock from the
UserStocks table. Because only the StockTrackerDB class can delete a stock
record, and only if a stock is no longer held by any user, the method for deleting
a Stocks table record is made private.
The SQL DELETE statement is used to delete from a given table all records
that match the search condition following the WHERE keyword. Much as with
the UPDATE statement, if the search condition matches multiple records, all
records are deleted. If the search condition is a primary key, then only one record
at most is deleted, because primary keys are unique.
The following step enters the code to delete a record from the Stocks table.
To Delete a Record in the Stocks Table
1. Enter lines 190 through 204 as shown in Figure 11-36 on page 736.
TextPad displays the delStock() method (Figure 11-45). The method is
private and can be called only from within the StockTrackerDB class.
SQL statement
DELETE
private
delStock()
method
FIGURE 11-45
Deleting a Stocks record is relatively simple. Because of the relationship
between the tables, deleting records from the Users or UserStocks tables is more
complex.
 
Search WWH ::




Custom Search