Java Reference
In-Depth Information
Listing 3.5
Wrapping the model logic with commands
public void initialize() B Initialize connects and validates.
throws IOException, DataException {
try {
Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver")
C Establish a
connection.
.newInstance();
String url = "jdbc:db2:board";
connection = DriverManager.getConnection (url);
} catch (Throwable theException) {
theException.printStackTrace();
}
}
public void execute() D Execute fires the business logic.
throws
IOException,
DataException {
try {
Statement statement = connection.createStatement();
result =
statement.executeQuery("SELECT subject, author, board" +
" from posts");
while (result.next()) {
subject.addElement(result.getString(SUBJECT_COLUMN));
author.addElement(result.getString(AUTHOR_COLUMN));
board.addElement(result.getString(BOARD_COLUMN));
}
result.close();
statement.close();
connection.close();
} catch (Throwable theException) {
theException.printStackTrace();
}
Execute
query
and get
results.
}
B The initialize method is used to establish connections and handle early valida-
tion of input parameters. Our command simply establishes the connection.
C For our example, we have no input parameters to validate, so we simply get our
database connection. In chapter 8, we will discuss connection pooling, which can
yield a significant performance boost.
Search WWH ::




Custom Search