Java Reference
In-Depth Information
String username = args[0];
String categoryTitle = args[1];
String title = args[2];
String message = args[3];
try {
UserDAO users = new UserDAO();
CategoryDAO categories = new CategoryDAO();
AdvertDAO adverts = new AdvertDAO();
User user = users.get(username);
Category category = categories.get(categoryTitle);
Advert advert = adverts.create(title, message, user);
category.addAdvert(advert);
categories.save(category);
DAO.close();
} catch (AdException e) {
e.printStackTrace();
}
}
}
Finally, in Listing 3-26, we make use of CategoryDAO to iterate over the categories, and
within these, the adverts drawn from the database. It is easy to see how this logic could
now be incorporated into a JSP file or servlet. It could even be used from within an EJB
session bean.
Listing 3-26. The Class to Display the Contents of the Database
package sample;
import java.util.Iterator;
import java.util.List;
import sample.dao.CategoryDAO;
import sample.dao.DAO;
import sample.entity.Advert;
import sample.entity.Category;
public class ListAdverts {
public static void main(String[] args) {
try {
List categories = new CategoryDAO().list();
Search WWH ::




Custom Search