Java Reference
In-Depth Information
Lines 30 to 37 : The connections need to be closed because connections are
expensive when it comes to the performance of the application.
Lines 39 to 144 : These lines are implementations of CRUD services in the
BookDAO interface.
Lines 67, 108, and 133 : You created a connection for each statement in the
CRUD services. You need to close these connections; keeping them open will
result in the poor performance of the application.
Client for the Data Access Layer
Now that your data access layer is ready, you will query it with a stand-alone Java application. In
Chapter 2, you will replace this Java app with a web application. Listing 1-9 illustrates the Java
application.
Listing 1-9. Stand-Alone Bookstore Java App
1. package com.apress.books.client;
2. import java.util.List;
3.
4. import com.apress.books.dao.BookDAO;
5. import com.apress.books.dao.BookDAOImpl;
6. import com.apress.books.model.Book;
7.
8. public class BookApp {
9. private static BookDAO bookDao = new BookDAOImpl();
10.
11. public static void main(String[] args) {
12. // List all books
13. System.err.println("Listing all Books:");
14. findAllBooks();
15. System.out.println();
16. // search book by keyword
17. System.err.println("Search book by keyword in book title : Groovy:");
18.
19. searchBooks("Groovy");
20. System.out.println();
21.
22. System.err.println("Search book by keyword in author's name : Josh:");
23.
24. searchBooks("Josh");
25.
26.
27. }
28.
29. private static void findAllBooks() {
30. List<Book> books = bookDao.findAllBooks();
31. for (Book book : books) {
32. System.out.println(book);
33. }
34. }
35. private static void searchBooks(String keyWord) {
 
Search WWH ::




Custom Search