From 82e095b443ca60603cf7261e73f41d5bfdb0ae56 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 10 May 2014 19:47:27 +0200 Subject: Move SQLException processing in processTweet to Main Huge diff comes from whitespace diff. Now the tweet is printed with its line number in error cases, and all further insertions are aborted. --- src/main/DataFiller.java | 68 +++++++++++++++++++++++------------------------- src/main/Main.java | 7 ++++- 2 files changed, 38 insertions(+), 37 deletions(-) (limited to 'src/main') diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java index 04d44db..8a010d0 100644 --- a/src/main/DataFiller.java +++ b/src/main/DataFiller.java @@ -51,46 +51,42 @@ public class DataFiller { } } - public void processTweet(Tweet tweet) { - try { - // ensure that the user and tweet are known before adding relations - QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, tweet); - m_insertProfile.executeUpdate(); - m_insertTweet.executeUpdate(); + public void processTweet(Tweet tweet) throws SQLException { + // ensure that the user and tweet are known before adding relations + QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, tweet); + m_insertProfile.executeUpdate(); + m_insertTweet.executeUpdate(); - for (Tweet.Hashtag hashtag : tweet.entities.hashtags) { - m_insertHash.setLong("tweetid", tweet.id); - m_insertHash.setString("hashtag", hashtag.text); - m_insertHash.executeUpdate(); - } - for (Tweet.Url url : tweet.entities.urls) { - m_insertTweetUrl.setLong("tweetid", tweet.id); - m_insertTweetUrl.setString("url", url.expanded_url); - m_insertTweetUrl.executeUpdate(); - } - for (Tweet.Mention mention : tweet.entities.user_mentions) { - m_insertMentions.setLong("tweetid", tweet.id); - m_insertMentions.setLong("userid", mention.id); - m_insertMentions.executeUpdate(); - } + for (Tweet.Hashtag hashtag : tweet.entities.hashtags) { + m_insertHash.setLong("tweetid", tweet.id); + m_insertHash.setString("hashtag", hashtag.text); + m_insertHash.executeUpdate(); + } + for (Tweet.Url url : tweet.entities.urls) { + m_insertTweetUrl.setLong("tweetid", tweet.id); + m_insertTweetUrl.setString("url", url.expanded_url); + m_insertTweetUrl.executeUpdate(); + } + for (Tweet.Mention mention : tweet.entities.user_mentions) { + m_insertMentions.setLong("tweetid", tweet.id); + m_insertMentions.setLong("userid", mention.id); + m_insertMentions.executeUpdate(); + } - User user = tweet.user; - if (user.entities != null) { - for (Tweet.Url url : tweet.entities.urls) { - m_insertUserUrl.setLong("userid", user.id); - m_insertUserUrl.setString("url", url.expanded_url); - m_insertUserUrl.executeUpdate(); - } + User user = tweet.user; + if (user.entities != null) { + for (Tweet.Url url : tweet.entities.urls) { + m_insertUserUrl.setLong("userid", user.id); + m_insertUserUrl.setString("url", url.expanded_url); + m_insertUserUrl.executeUpdate(); } + } - // determine the user's perception of the brand - List brands = getBrands(tweet); - for (String brand : brands) { - QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand); - m_insertBrand.executeUpdate(); - } - } catch (SQLException ex) { - Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex); + // determine the user's perception of the brand + List brands = getBrands(tweet); + for (String brand : brands) { + QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand); + m_insertBrand.executeUpdate(); } } diff --git a/src/main/Main.java b/src/main/Main.java index b5df23f..bd4055f 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -63,14 +63,19 @@ public class Main { } private void tweetsToDb(ITweetReader reader) throws IOException { - Tweet tweet; + Tweet tweet = null; + int tweetNo = 1; try (Connection connection = cb.create()) { /* create the object that fills the database */ DataFiller filler = new DataFiller(connection); while ((tweet = reader.getTweet()) != null) { filler.processTweet(tweet); + ++tweetNo; } } catch (SQLException ex) { + if (tweet != null) { + System.err.println("Faulty tweet: " + tweet); + } Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "DB error", ex); } -- cgit v1.2.1