Java Reference
In-Depth Information
Because a StockTrackerDB object opens a connection to the database as soon
as it is created, that connection should be closed when the StockTrackerDB
object no longer is needed by the application program. The application program
then calls a method to indicate when the data access class no longer is needed,
and the connection is closed. Lines 47 through 51 comprise the close() method
of the data access class. Line 49 closes the Connection, and line 50 sets the refer-
ence to null.
The following step enters code to create the close() method to close the con-
nection for the data access class, StockTrackerDB.
To Close a Connection
1. Enter lines 46 through 52 as shown in Figure 11-36 on page 734.
The connection to the StockTracker database is closed (Figure 11-39).
StockTrackerDB
close() method
Connection close() method
FIGURE 11-39
In the MakeDB class, methods were written to serialize and deserialize an
object. These methods were necessary to be able to store a Password object in the
database. The StockTrackerDB class also must be able to serialize and deserialize a
Password object. The MakeDB methods were public and static, but only because
that class called those methods directly from its main() method. The MakeDB is
considered a one-time utility and, therefore, cannot be expected to be available
whenever the StockTrackerDB class is used. Additionally, these methods are used
only within the StockTrackerDB class, and so should be made private. Otherwise,
the methods are exactly the same as the corresponding methods written earlier.
Search WWH ::




Custom Search