Learn Java for Web Development

(Tina Meador) #1
CHAPTER 1: Introducing Java Web Development 33

    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. import com.apress.books.dao.BookDAO;

  4. import com.apress.books.dao.BookDAOImpl;

  5. import com.apress.books.model.Book;



  6. public class BookApp {

  7. private static BookDAO bookDao = new BookDAOImpl();



  8. public static void main(String[] args) {

  9. // List all books

  10. System.err.println("Listing all Books:");

  11. findAllBooks();

  12. System.out.println();

  13. // search book by keyword

  14. System.err.println("Search book by keyword in book title : Groovy:");



  15. searchBooks("Groovy");

  16. System.out.println();



  17. System.err.println("Search book by keyword in author's name : Josh:");



  18. searchBooks("Josh");





  19. }



  20. private static void findAllBooks() {

  21. List books = bookDao.findAllBooks();

  22. for (Book book : books) {

  23. System.out.println(book);

  24. }

  25. }

  26. private static void searchBooks(String keyWord) {

Free download pdf