summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-10 19:47:27 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-10 19:47:27 +0200
commit82e095b443ca60603cf7261e73f41d5bfdb0ae56 (patch)
treeb493dcc183dae0ebc99fb889914d889821726b48
parent2b673df3123e3e1fd4edd6861abe316336e1d69c (diff)
downloadDatafiller-82e095b443ca60603cf7261e73f41d5bfdb0ae56.tar.gz
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.
-rw-r--r--src/main/DataFiller.java68
-rw-r--r--src/main/Main.java7
2 files changed, 38 insertions, 37 deletions
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<String> 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<String> 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);
}