CHAPTER 1: Introducing Java Web Development 29
Listing 1-7. BookDAO Interface
- package com.apress.books.dao;
- import java.util.List;
- import com.apress.books.model.Book;
- import com.apress.books.model.Category;
- public interface BookDAO {
- public List
findAllBooks();
- public List
searchBooksByKeyword(String keyWord);
- public List
findAllCategories();
- public void insert(Book book);
- public void update(Book book);
- public void delete(Long bookId);
- }
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
- package com.apress.books.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.apress.books.model.Author;
- import java.apress.books.model.Book;