diff --git a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java index 22d7f8b..e25bfa7 100644 --- a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java @@ -929,7 +929,7 @@ public List getAllTags() { @Override public List getRecommendationsWithTag(String tag) { ArrayList list = new ArrayList<>(); - + list.addAll(getBooksWithTag(tag)); list.addAll(getVideosWithTag(tag)); list.addAll(getPodcastsWithTag(tag)); @@ -945,21 +945,17 @@ public List getBooksWithTag(String tag) { " where tagText = ?"; ArrayList books = new ArrayList<>(); + Connection connection = this.connect(); + ResultSet result = getResultsForTagSearch(tag, sql, connection); try { - Connection connection = this.connect(); - PreparedStatement statement = connection.prepareStatement(sql); - statement.setString(1, tag); - ResultSet result = statement.executeQuery(); while (result.next()) { books.add(new BookRecommendation(result.getInt("id"), result.getString("author"), result.getString("title"), result.getString("description"), result.getString("isbn"), result.getInt("pageCount"), result.getString("created"))); } - connection.close(); } catch (SQLException e) { - System.out.println(e.getMessage()); + e.printStackTrace(); } - return books; } @@ -971,19 +967,16 @@ public List getVideosWithTag(String tag) { " where tags.tagText = ?"; ArrayList videos = new ArrayList<>(); + Connection connection = this.connect(); + ResultSet result = getResultsForTagSearch(tag, sql, connection); try { - Connection connection = this.connect(); - PreparedStatement statement = connection.prepareStatement(sql); - statement.setString(1, tag); - ResultSet result = statement.executeQuery(); while (result.next()) { videos.add(new VideoRecommendation(result.getInt("id"), result.getString("url"), result.getString("title"), result.getString("description"), result.getString("created"))); } - connection.close(); } catch (SQLException e) { - System.out.println(e.getMessage()); + e.printStackTrace(); } return videos; } @@ -995,24 +988,21 @@ public List getPodcastsWithTag(String tag) { " join tags on tags_id = tags.id " + " where tags.tagText = ?"; ArrayList podcasts = new ArrayList<>(); + Connection connection = this.connect(); + ResultSet result = getResultsForTagSearch(tag, sql, connection); try { - Connection connection = this.connect(); - PreparedStatement statement = connection.prepareStatement(sql); - statement.setString(1, tag); - - ResultSet result = statement.executeQuery(); while (result.next()) { podcasts.add(new PodcastRecommendation(result.getInt("id"), result.getString("author"), result.getString("title"), result.getString("description"), result.getString("name"), result.getString("created"))); } - connection.close(); } catch (SQLException e) { - System.out.println(e.getMessage()); + e.printStackTrace(); } return podcasts; } + public List getBlogsWithTag(String tag) { String sql = "SELECT Blogs.id, url, author, title, description, created" + " from Blogs" + @@ -1020,21 +1010,30 @@ public List getBlogsWithTag(String tag) { " join tags on tags_id = tags.id " + " where tags.tagText = ?"; ArrayList blogs = new ArrayList<>(); + Connection connection = this.connect(); + ResultSet result = getResultsForTagSearch(tag, sql, connection); try { - Connection connection = this.connect(); - PreparedStatement statement = connection.prepareStatement(sql); - statement.setString(1, tag); - - ResultSet result = statement.executeQuery(); while (result.next()) { blogs.add(new BlogRecommendation(result.getInt("id"), result.getString("author"), result.getString("url"), result.getString("title"), result.getString("description"), result.getString("created"))); } - connection.close(); } catch (SQLException e) { - System.out.println(e.getMessage()); + e.printStackTrace(); } return blogs; } + + private ResultSet getResultsForTagSearch(String tag, String sql, Connection connection) { + try { + PreparedStatement statement = connection.prepareStatement(sql); + statement.setString(1, tag); + + ResultSet result = statement.executeQuery(); + return result; + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + return null; + } }