From d53522e78e6b8ada72e8341e67a2e94fc917af1a Mon Sep 17 00:00:00 2001 From: Christian F Date: Fri, 25 May 2018 22:11:31 +0200 Subject: [PATCH] - use UPSERT for website links as well --- src/main/java/mSearch/daten/DatenFilm.java | 66 ++++++++++++---------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/main/java/mSearch/daten/DatenFilm.java b/src/main/java/mSearch/daten/DatenFilm.java index 8a74372e..707d131b 100644 --- a/src/main/java/mSearch/daten/DatenFilm.java +++ b/src/main/java/mSearch/daten/DatenFilm.java @@ -265,31 +265,6 @@ public void setDescription(final String desc) { } } - private void writeDescriptionToDatabase(String desc) { - try (Connection connection = PooledDatabaseConnection.getInstance().getConnection(); - PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO description VALUES (?,?)"); - PreparedStatement updateStatement = connection.prepareStatement("UPDATE description SET desc=? WHERE id =?") - ) { - String cleanedDesc = cleanDescription(desc, arr[FILM_THEMA], getTitle()); - cleanedDesc = StringUtils.replace(cleanedDesc, "\n", "
"); - - updateStatement.setString(1, cleanedDesc); - updateStatement.setInt(2, databaseFilmNumber); - updateStatement.executeUpdate(); - - insertStatement.setInt(1, databaseFilmNumber); - insertStatement.setString(2, cleanedDesc); - insertStatement.execute(); - - - } catch (SQLIntegrityConstraintViolationException ignored) { - //this will happen in UPSERT operation - } catch (SQLException ex) { - logger.error(ex); - } - - } - public String getWebsiteLink() { String res; @@ -332,15 +307,48 @@ public void setWebsiteLink(String link) { private void writeWebsiteLinkToDatabase(String link) { try (Connection connection = PooledDatabaseConnection.getInstance().getConnection(); - PreparedStatement statement = connection.prepareStatement("INSERT INTO website_links VALUES (?,?)")) { - statement.setInt(1, databaseFilmNumber); - statement.setString(2, link); - statement.executeUpdate(); + PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO website_links VALUES (?,?)"); + PreparedStatement updateStatement = connection.prepareStatement("UPDATE website_links SET link=? WHERE id=?")) { + + updateStatement.setString(1, link); + updateStatement.setInt(2, databaseFilmNumber); + updateStatement.executeUpdate(); + + insertStatement.setInt(1, databaseFilmNumber); + insertStatement.setString(2, link); + insertStatement.executeUpdate(); + } catch (SQLIntegrityConstraintViolationException ignored) { } catch (SQLException ex) { logger.error(ex); } } + private void writeDescriptionToDatabase(String desc) { + try (Connection connection = PooledDatabaseConnection.getInstance().getConnection(); + PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO description VALUES (?,?)"); + PreparedStatement updateStatement = connection.prepareStatement("UPDATE description SET desc=? WHERE id =?") + ) { + String cleanedDesc = cleanDescription(desc, arr[FILM_THEMA], getTitle()); + cleanedDesc = StringUtils.replace(cleanedDesc, "\n", "
"); + + updateStatement.setString(1, cleanedDesc); + updateStatement.setInt(2, databaseFilmNumber); + updateStatement.executeUpdate(); + + insertStatement.setInt(1, databaseFilmNumber); + insertStatement.setString(2, cleanedDesc); + insertStatement.execute(); + + + } catch (SQLIntegrityConstraintViolationException ignored) { + //this will happen in UPSERT operation + } catch (SQLException ex) { + logger.error(ex); + } + + } + + private String cleanDescription(String s, String thema, String titel) { // die Beschreibung auf x Zeichen beschränken