Java Reference
In-Depth Information
After the validation, we connect to the database and then clean up:
public void execute()
throws
IOException,
DataException {
try {
query =
"INSERT INTO POSTS values ('"
+ getSubject()
+ "', (select max(number) from posts) + 1, '"
+ getAuthor()
+ "', '"
+ getPostText()
+ "', current timestamp, "
+ getParent()
+ ", '"
+ getBoard()
+ "')";
In our execute method, we add a record to the database. The logic is notice-
ably simpler than usual because we have already done the validation of the ele-
ments and we catch the database exceptions elsewhere.
Statement statement = connection.createStatement();
result = statement.executeQuery(query);
result.close();
connection.close();
statement.close();
} catch (Throwable theException) {
theException.printStackTrace();
}
}
}
We then execute the query, clean up, and catch exceptions.
The controller for AddPost
AddPostController is similar to the other commands, with a new twist: We
will dispatch an existing servlet instead of a new JSP . As usual, we'll focus on
the action in performTask :
public void performTask(
HttpServletRequest request,
HttpServletResponse response) {
try {
String board = request.getParameter ("board");
Search WWH ::




Custom Search