Java Reference
In-Depth Information
5. Enter lines 1 through 16 as shown in Figure 11-36. In the comments,
insert your own name as programmer and enter the current date.
TextPad displays the code for the initial comments, import statements, class
header, and instance variable for the StockTrackerDB class (Figure 11-37). The
instance variable, con, is accessible only by instance methods in the class.
StockTrackerDB
.java
import statements
FIGURE 11-37
private instance
variable
Because the StockTrackerDB class is a model, or representation, of the data-
base to another program, it should connect to the database when an instance of
the class is created. Therefore, the StockTrackerDB constructor loads the JDBC™
driver and makes the database connection, similar to what was done earlier. The
connection, once made, is referenced by the instance variable, con, defined on
line 15 and available for use until the data access class no longer is needed by the
user application.
The following step enters code to create the constructor for the data access
class, StockTrackerDB.
To Create a Data Access Class Constructor
1. Enter lines 17 through 45 as shown in Figure 11-36 on pages 733 and
734.
The constructor loads the JDBC-ODBC driver and obtains a Connection
object for the StockTracker database (Figure 11-38).
Class.forName()
method
JDBC-ODBC
driver
DriverManager
getConnection()
method
FIGURE 11-38
 
Search WWH ::




Custom Search