diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-07 14:02:23 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-07 14:02:23 +0200 |
commit | a9de546d1867c4a2f130cf55c208719eb6b27f54 (patch) | |
tree | 45d22cd9a7f402205ff29f48e37d153e6e51e96b /src | |
parent | ae7f9a9708b5e2504852262c2af245e0ea68275b (diff) | |
download | Datafiller-a9de546d1867c4a2f130cf55c208719eb6b27f54.tar.gz |
Removed the extranous DBQuery and simpel property
* InputReader reads from Scanner.
Diffstat (limited to 'src')
-rw-r--r-- | src/database/DBConnection.java | 39 | ||||
-rw-r--r-- | src/database/DBQuery.java | 20 | ||||
-rw-r--r-- | src/database/QueryUtils.java | 36 | ||||
-rw-r--r-- | src/io/InputReader.java | 7 | ||||
-rw-r--r-- | src/main/DataFiller.java | 35 | ||||
-rw-r--r-- | src/main/Main.java | 9 |
6 files changed, 84 insertions, 62 deletions
diff --git a/src/database/DBConnection.java b/src/database/DBConnection.java index 4227fb3..330c2a5 100644 --- a/src/database/DBConnection.java +++ b/src/database/DBConnection.java @@ -1,45 +1,52 @@ - package database; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * Create a persistent database connection. + * * @author Maurice Laveaux */ public class DBConnection { - + /* The interface to the postgresql database connection. */ private Connection m_connection; - - public DBConnection(final String hostaddress, - final String port, - final String databasename, - final String username, - final String password) { - + + public DBConnection(final String hostaddress, + final String port, + final String databasename, + final String username, + final String password) { + String url = "jdbc:postgresql://" + hostaddress + ":" + port + "/" + databasename; - - try { + + try { m_connection = DriverManager.getConnection(url, username, password); } catch (SQLException ex) { //TODO: retry when db connection fails or something. throw new RuntimeException("cannot connect to host: " + url); } } - - public void prepare(DBQuery query) { - + + /** + * prepares a statement. + * + * @param query The query to prepare. + * @return A prepared statement. + */ + public PreparedStatement create(final String query) throws SQLException { + return m_connection.prepareStatement(query); } - + /** * Closes the connection if it exists. */ - public void close() { + public void close() { if (m_connection != null) { try { m_connection.close(); diff --git a/src/database/DBQuery.java b/src/database/DBQuery.java deleted file mode 100644 index 602af97..0000000 --- a/src/database/DBQuery.java +++ /dev/null @@ -1,20 +0,0 @@ -package database; - -import java.sql.PreparedStatement; - -/** - * An interface to known query objects. - * @author Maurice Laveaux - */ -public class DBQuery { - - protected PreparedStatement m_prepared; - - public DBQuery(final String query) { - - } - - public void execute() { - - } -} diff --git a/src/database/QueryUtils.java b/src/database/QueryUtils.java index a120d1c..5b08655 100644 --- a/src/database/QueryUtils.java +++ b/src/database/QueryUtils.java @@ -1,5 +1,8 @@ package database; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import org.json.JSONException; import org.json.JSONObject; /** @@ -14,27 +17,32 @@ public class QueryUtils { * * @return A valid database query. */ - public static DBQuery insertProfile() { - String query = "INSERT INTO twitteruser (userid,displayname,timezone,tweetcount," + public static String insertProfile() { + return "INSERT INTO twitteruser (userid,displayname,timezone,tweetcount," + "followercount,followedcount,location) " + "SELECT ?, ?, ?, ?, ?, ?, ? " + "WHERE NOT EXISTS " + "(SELECT * FROM twitteruser WHERE userid= ? )"; - return new DBQuery(query); } - public static DBQuery insertTweet() { - String query = "INSERT INTO tweet (tweetid,createdat,favcount,retweetcount,text) " - + "SELECT ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT * FROM tweet WHERE tweetid= ? )"; - return new DBQuery(query); + public static String insertTweet() { + return "INSERT INTO tweet (tweetid,createdat,favcount,retweetcount,text) " + + "SELECT ?, ?, ?, ?, ? " + + "WHERE NOT EXISTS " + + "(SELECT * FROM tweet WHERE tweetid=? )"; + } + + public static String insetHash() { + return "INSERT INTO hashtag (tweetid, hashtag) " + + "SELECT ?, ? "; } - public static void setInsertParams(DBQuery query, JSONObject tweet) { - /*query.getPrepared().setLong(1, tweet.getLong("id")); - pst.setString(2, tweet.getString("created_at")); - pst.setLong(3, tweet.getLong("favorite_count")); - pst.setLong(4, tweet.getLong("retweet_count")); - pst.setString(5, tweet.getString("text")); - pst.setLong(6, tweet.getLong("id"));*/ + public static void setInsertParams(PreparedStatement statement, JSONObject tweet) throws JSONException, SQLException { + statement.setLong( 1, tweet.getLong( "id")); + statement.setString(2, tweet.getString("created_at")); + statement.setLong( 3, tweet.getLong( "favorite_count")); + statement.setLong( 4, tweet.getLong( "retweet_count")); + statement.setString(5, tweet.getString("text")); + statement.setLong( 6, tweet.getLong( "id")); } } diff --git a/src/io/InputReader.java b/src/io/InputReader.java index ef22c97..0926022 100644 --- a/src/io/InputReader.java +++ b/src/io/InputReader.java @@ -32,7 +32,12 @@ public class InputReader { while (input.hasNext()) { try { - JSONObject tweet = new JSONObject(input.nextLine()); + String line = input.nextLine(); + if(line.equals("exit")) { + return; + } + + JSONObject tweet = new JSONObject(line); if (tweet.has("tweet-id")) { throw new JSONException("not a valid tweet JSON object"); diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java index 461bd21..20fcf3a 100644 --- a/src/main/DataFiller.java +++ b/src/main/DataFiller.java @@ -1,8 +1,12 @@ package main; import database.DBConnection; -import database.DBQuery; import database.QueryUtils; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.json.JSONException; import org.json.JSONObject; /** @@ -18,9 +22,14 @@ public class DataFiller implements ResultListener { private final DBConnection m_connection; /** - * A single insert tweet that can be used often. + * A single insert tweet that can be used. */ - private DBQuery m_insertTweet; + private PreparedStatement m_insertTweet; + + /** + * A single insert profiles that can be used. + */ + private PreparedStatement m_insertProfile; /** * Create the datafiller object. @@ -28,16 +37,22 @@ public class DataFiller implements ResultListener { * @param connection The database connection to use. */ public DataFiller(DBConnection connection) { - m_insertTweet = QueryUtils.insertTweet(); - - m_connection = connection; - m_connection.prepare(m_insertTweet); + try { + m_connection = connection; + m_insertTweet = m_connection.create(QueryUtils.insertTweet()); + } catch (SQLException ex) { + throw new RuntimeException(ex.getMessage()); + } } @Override public void tweetReceived(JSONObject tweet) { - QueryUtils.setInsertParams(m_insertTweet, tweet); - - m_insertTweet.execute(); + try { + QueryUtils.setInsertParams(m_insertTweet, tweet); + + m_insertTweet.executeUpdate(); + } catch (SQLException | JSONException ex) { + Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex); + } } } diff --git a/src/main/Main.java b/src/main/Main.java index dec057a..62a5fe4 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -32,7 +32,7 @@ public class Main { parseGlobalOptions(args); if (m_hostaddress == null) { - throw new IllegalArgumentException("Missing --ip to specify the hostaddress."); + throw new IllegalArgumentException("Missing --dbhost to specify the hostaddress."); } DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02"); @@ -48,6 +48,8 @@ public class Main { } connection.close(); + + System.out.print("exit succesfull."); } private void parseGlobalOptions(String[] args) { @@ -64,6 +66,10 @@ public class Main { m_filename = getParam(args, i); } } + + if (args.length == 0) { + throw new IllegalArgumentException("No parameters specified, see --help for usage info."); + } } /** Read an extra option for a command. */ @@ -76,6 +82,7 @@ public class Main { } } + /** Print some useful help messages. */ private void printHelp() { for (String line : HELP) { System.out.println(line); |