summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/DataFiller.java34
-rw-r--r--src/main/Main.java68
2 files changed, 56 insertions, 46 deletions
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java
index a65a032..6e1ba9d 100644
--- a/src/main/DataFiller.java
+++ b/src/main/DataFiller.java
@@ -1,8 +1,8 @@
package main;
import data.Tweet;
-import database.DBConnection;
import database.QueryUtils;
+import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -20,57 +20,57 @@ public class DataFiller {
/**
* The main database connection to fill.
*/
- private final DBConnection m_connection;
+ private final Connection m_connection;
/**
* A single insert tweet that can be used.
*/
- private PreparedStatement m_insertTweet;
+ private final PreparedStatement m_insertTweet;
/**
* A single insert profiles that can be used.
*/
- private PreparedStatement m_insertProfile;
+ private final PreparedStatement m_insertProfile;
/**
* A single insert ispostedby that can be used.
*/
- private PreparedStatement m_insertPosted;
+ private final PreparedStatement m_insertPosted;
/**
* A single insert brand that can be used.
*/
- private PreparedStatement m_insertBrand;
+ private final PreparedStatement m_insertBrand;
/**
* A single insert hashtag that can be used.
*/
- private PreparedStatement m_insertHash;
+ private final PreparedStatement m_insertHash;
/**
* A single insert url that can be used.
*/
- private PreparedStatement m_insertUrl;
+ private final PreparedStatement m_insertUrl;
/**
* A single insert url that can be used.
*/
- private PreparedStatement m_insertMentions;
+ private final PreparedStatement m_insertMentions;
/**
* Create the datafiller object.
*
* @param connection The database connection to use.
*/
- public DataFiller(DBConnection connection) {
+ public DataFiller(Connection connection) {
try {
m_connection = connection;
- m_insertTweet = m_connection.create(QueryUtils.insertTweet());
- m_insertProfile = m_connection.create(QueryUtils.insertProfile());
- m_insertPosted = m_connection.create(QueryUtils.insertPosted());
- m_insertBrand = m_connection.create(QueryUtils.insertBrand());
- m_insertHash = m_connection.create(QueryUtils.insertHash());
- m_insertUrl = m_connection.create(QueryUtils.insertUrl());
- m_insertMentions = m_connection.create(QueryUtils.insertMentions());
+ m_insertTweet = m_connection.prepareStatement(QueryUtils.insertTweet());
+ m_insertProfile = m_connection.prepareStatement(QueryUtils.insertProfile());
+ m_insertPosted = m_connection.prepareStatement(QueryUtils.insertPosted());
+ m_insertBrand = m_connection.prepareStatement(QueryUtils.insertBrand());
+ m_insertHash = m_connection.prepareStatement(QueryUtils.insertHash());
+ m_insertUrl = m_connection.prepareStatement(QueryUtils.insertUrl());
+ m_insertMentions = m_connection.prepareStatement(QueryUtils.insertMentions());
} catch (SQLException ex) {
throw new RuntimeException(ex.getMessage());
diff --git a/src/main/Main.java b/src/main/Main.java
index 5a8e137..ba59f8b 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -1,13 +1,16 @@
package main;
-import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import data.Tweet;
-import database.DBConnection;
+import database.ConnectionBuilder;
import io.FileTweetReader;
import io.ITweetReader;
import io.TweetReader;
import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* The main class.
@@ -40,35 +43,42 @@ public class Main {
throw new IllegalArgumentException("Missing --dbhost to specify the hostaddress.");
}
- DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02");
- /* create the object that fills the database */
- DataFiller filler = new DataFiller(connection);
-
- ITweetReader reader = null;
- try {
- if (m_filename == null) {
- reader = new TweetReader(System.in);
- } else {
- reader = new FileTweetReader(m_filename);
- }
-
- Tweet tweet;
- while ((tweet = reader.getTweet()) != null) {
- filler.processTweet(tweet);
- }
- } catch (JsonSyntaxException ex) {
- System.err.println("Got an invalid tweet: " + ex);
- } catch (IOException ex) {
- System.err.println("Cannot open tweets: " + ex);
- } finally {
- if (reader != null) {
- reader.close();
+ ConnectionBuilder cb = new ConnectionBuilder()
+ .setServerName(m_hostaddress)
+ .setUsername("postgres")
+ .setPassword("2IOC02")
+ .setDbName("Twitter");
+
+ try (Connection connection = cb.create()) {
+
+ /* create the object that fills the database */
+ DataFiller filler = new DataFiller(connection);
+
+ ITweetReader reader = null;
+ try {
+ if (m_filename == null) {
+ reader = new TweetReader(System.in);
+ } else {
+ reader = new FileTweetReader(m_filename);
+ }
+
+ Tweet tweet;
+ while ((tweet = reader.getTweet()) != null) {
+ filler.processTweet(tweet);
+ }
+ } catch (JsonSyntaxException ex) {
+ System.err.println("Got an invalid tweet: " + ex);
+ } catch (IOException ex) {
+ System.err.println("Cannot open tweets: " + ex);
+ } finally {
+ if (reader != null) {
+ reader.close();
+ }
}
+ } catch (SQLException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
+ "Query error", ex);
}
-
- connection.close();
-
- System.out.print("exit succesfull.");
}
private void parseGlobalOptions(String[] args) {