Java Reference
In-Depth Information
66. } finally {
67. closeConnection(connection);
68. }
69. return result;
70. }
71.
72.
73. public List<Book> searchBooksByKeyword(String keyWord) {
74. List<Book> result = new ArrayList<>();
75. List<Author> authorList = new ArrayList<>();
76.
77. String sql = "select * from book inner join author on book.id = author.book_id"
78. + " where book_title like '%"
79. + keyWord.trim()
80. + "%'"
81. + " or first_name like '%"
82. + keyWord.trim()
83. + "%'"
84. + " or last_name like '%" + keyWord.trim() + "%'";
85.
86. Connection connection = null;
87. try {
88.
89. connection = getConnection();
90. PreparedStatement statement = connection.prepareStatement(sql);
91. ResultSet resultSet = statement.executeQuery();
92. while (resultSet.next()) {
93. Book book = new Book();
94. Author author = new Author();
95. book.setId(resultSet.getLong("id"));
96. book.setBookTitle(resultSet.getString("book_title"));
97. book.setPublisherName(resultSet.getString("publisher"));
98. author.setFirstName(resultSet.getString("first_name"));
99. author.setLastName(resultSet.getString("last_name"));
100. author.setBookId(resultSet.getLong("book_id"));
101. authorList.add(author);
102. book.setAuthors(authorList);
103. result.add(book);
104. }
105. } catch (SQLException ex) {
106. ex.printStackTrace();
107. } finally {
108. closeConnection(connection);
109. }
110.
111. return result;
112. }
113.
114. public List<Category> findAllCategories() {
115. List<Category> result = new ArrayList<>();
116. String sql = "select * from category";
117.
Search WWH ::




Custom Search