CHAPTER 1: Introducing Java Web Development 31
- } finally {
- closeConnection(connection);
- }
- return result;
- }
- public List searchBooksByKeyword(String keyWord) {
- List result = new ArrayList<>();
- List authorList = new ArrayList<>();
- String sql = "select * from book inner join author on book.id = author.book_id"
- " where book_title like '%"
- " or last_name like '%" + keyWord.trim() + "%'";
- Connection connection = null;
- try {
- connection = getConnection();
- PreparedStatement statement = connection.prepareStatement(sql);
- ResultSet resultSet = statement.executeQuery();
- while (resultSet.next()) {
- Book book = new Book();
- Author author = new Author();
- book.setId(resultSet.getLong("id"));
- book.setBookTitle(resultSet.getString("book_title"));
- book.setPublisherName(resultSet.getString("publisher"));
- author.setFirstName(resultSet.getString("first_name"));
- author.setLastName(resultSet.getString("last_name"));
- author.setBookId(resultSet.getLong("book_id"));
- authorList.add(author);
- book.setAuthors(authorList);
- result.add(book);
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- } finally {
- closeConnection(connection);
- }
- return result;
- }
- public List findAllCategories() {
- List result = new ArrayList<>();
- String sql = "select * from category";