Learn Java for Web Development

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

Listing 1-7. BookDAO Interface



  1. package com.apress.books.dao;



  2. import java.util.List;



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

  4. import com.apress.books.model.Category;



  5. public interface BookDAO {

  6. public ListfindAllBooks();



  7. public ListsearchBooksByKeyword(String keyWord);



  8. public ListfindAllCategories();



  9. public void insert(Book book);



  10. public void update(Book book);



  11. public void delete(Long bookId);



  12. }


   Line 9: This is the findAllBooks() method for listing all the books from the
database.
 Line 11: SearchBooksByKeyword(String keyWord) allows the user to search
books by keyword in the title of the book or by the first and last names of the
author.
 Line 13: findAllCategories() is required by the application to provide a
categorized listing of books.

The methods in this interface correspond to the CRUD terms (in other words, create, read, update,
and delete) of the application. Listing 1-8 illustrates the implementation of the BookDAO interface.


Listing 1-8. Implementation of the BookDAO Interface



  1. package com.apress.books.dao;



  2. import java.sql.Connection;

  3. import java.sql.DriverManager;

  4. import java.sql.PreparedStatement;

  5. import java.sql.ResultSet;

  6. import java.sql.SQLException;

  7. import java.sql.Statement;

  8. import java.util.ArrayList;

  9. import java.util.List;



  10. import java.apress.books.model.Author;

  11. import java.apress.books.model.Book;

Free download pdf