Skip to content

Commit

Permalink
Centralised database junk
Browse files Browse the repository at this point in the history
  • Loading branch information
predominant committed Jun 2, 2012
1 parent 3cf7daa commit 2deaed8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
61 changes: 61 additions & 0 deletions RouteMeServer/src/routeme/server/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package routeme.server;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import routeme.server.response.ResponseServer;

public class DatabaseManager {

protected Properties properties;

public DatabaseManager() {
try {
System.out.println("Resolving MySQL driver");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Unable to load MySQL Driver");
System.exit(1);
}

this.properties = this.getProperties();
}

public Connection getConnection() {
Connection conn = null;
Properties prop = this.properties;

try {
System.out.println("Attempting MySQL connection");
conn = DriverManager.getConnection(
"jdbc:mysql://" +
prop.getProperty("hostname") + "/" +
prop.getProperty("database") + "?" +
"user=" + prop.getProperty("username") + "&" +
"password=" + prop.getProperty("password"));

} catch (SQLException e) {
System.out.println("Failed to connect to MySQL database");
System.exit(3);
}

return conn;
}

protected Properties getProperties() {
Properties prop = new Properties();

try {
prop.load(new FileInputStream("database.properties"));
} catch (IOException e) {
System.out.println("Failed to load database.properties file");
System.exit(2);
}
return prop;
}

}
48 changes: 8 additions & 40 deletions RouteMeServer/src/routeme/server/response/ResponseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
import java.sql.Statement;
import java.util.Properties;

import routeme.server.DatabaseManager;
import twitter4j.Tweet;

public class ResponseServer {

DatabaseManager db;

public ResponseServer() {
try {
System.out.println("Resolving MySQL driver");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Unable to load MySQL Driver");
System.exit(1);
}
this.db = new DatabaseManager();
}

public void start(Properties prop) {
Connection conn = this.getConnection(prop);
public void start() {
Connection conn = this.db.getConnection();

while (true) {
try {
Expand Down Expand Up @@ -80,26 +77,6 @@ protected boolean processTweet(String tweet) {
return true;
}

protected Connection getConnection(Properties prop) {
Connection conn = null;

try {
System.out.println("Attempting MySQL connection");
conn = DriverManager.getConnection(
"jdbc:mysql://" +
prop.getProperty("hostname") + "/" +
prop.getProperty("database") + "?" +
"user=" + prop.getProperty("username") + "&" +
"password=" + prop.getProperty("password"));

} catch (SQLException e) {
System.out.println("Failed to connect to MySQL database");
System.exit(3);
}

return conn;
}

/**
* @param args
*/
Expand All @@ -108,16 +85,7 @@ public static void main(String[] args) {
System.out.println("Starting RouteMe ResponseServer");
System.out.println("=============================================");

Properties prop = new Properties();

try {
prop.load(new FileInputStream("database.properties"));
ResponseServer server = new ResponseServer();
server.start(prop);
} catch (IOException e) {
System.out.println("Failed to load database.properties file");
System.exit(2);
}

ResponseServer server = new ResponseServer();
server.start();
}
}
13 changes: 13 additions & 0 deletions RouteMeServer/src/routeme/server/timeline/TimelineServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package routeme.server.timeline;

public class TimelineServer {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

0 comments on commit 2deaed8

Please sign in to comment.