Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Centralised database junk

  • Loading branch information...
commit 2deaed86c973915a17fd2ef49200d9d5970c66c0 1 parent 3cf7daa
Graham Weldon authored
61 RouteMeServer/src/routeme/server/DatabaseManager.java
... ... @@ -0,0 +1,61 @@
  1 +package routeme.server;
  2 +
  3 +import java.io.FileInputStream;
  4 +import java.io.IOException;
  5 +import java.sql.Connection;
  6 +import java.sql.DriverManager;
  7 +import java.sql.SQLException;
  8 +import java.util.Properties;
  9 +
  10 +import routeme.server.response.ResponseServer;
  11 +
  12 +public class DatabaseManager {
  13 +
  14 + protected Properties properties;
  15 +
  16 + public DatabaseManager() {
  17 + try {
  18 + System.out.println("Resolving MySQL driver");
  19 + Class.forName("com.mysql.jdbc.Driver");
  20 + } catch (ClassNotFoundException e) {
  21 + System.out.println("Unable to load MySQL Driver");
  22 + System.exit(1);
  23 + }
  24 +
  25 + this.properties = this.getProperties();
  26 + }
  27 +
  28 + public Connection getConnection() {
  29 + Connection conn = null;
  30 + Properties prop = this.properties;
  31 +
  32 + try {
  33 + System.out.println("Attempting MySQL connection");
  34 + conn = DriverManager.getConnection(
  35 + "jdbc:mysql://" +
  36 + prop.getProperty("hostname") + "/" +
  37 + prop.getProperty("database") + "?" +
  38 + "user=" + prop.getProperty("username") + "&" +
  39 + "password=" + prop.getProperty("password"));
  40 +
  41 + } catch (SQLException e) {
  42 + System.out.println("Failed to connect to MySQL database");
  43 + System.exit(3);
  44 + }
  45 +
  46 + return conn;
  47 + }
  48 +
  49 + protected Properties getProperties() {
  50 + Properties prop = new Properties();
  51 +
  52 + try {
  53 + prop.load(new FileInputStream("database.properties"));
  54 + } catch (IOException e) {
  55 + System.out.println("Failed to load database.properties file");
  56 + System.exit(2);
  57 + }
  58 + return prop;
  59 + }
  60 +
  61 +}
48 RouteMeServer/src/routeme/server/response/ResponseServer.java
@@ -9,22 +9,19 @@
9 9 import java.sql.Statement;
10 10 import java.util.Properties;
11 11
  12 +import routeme.server.DatabaseManager;
12 13 import twitter4j.Tweet;
13 14
14 15 public class ResponseServer {
15 16
  17 + DatabaseManager db;
  18 +
16 19 public ResponseServer() {
17   - try {
18   - System.out.println("Resolving MySQL driver");
19   - Class.forName("com.mysql.jdbc.Driver");
20   - } catch (ClassNotFoundException e) {
21   - System.out.println("Unable to load MySQL Driver");
22   - System.exit(1);
23   - }
  20 + this.db = new DatabaseManager();
24 21 }
25 22
26   - public void start(Properties prop) {
27   - Connection conn = this.getConnection(prop);
  23 + public void start() {
  24 + Connection conn = this.db.getConnection();
28 25
29 26 while (true) {
30 27 try {
@@ -80,26 +77,6 @@ protected boolean processTweet(String tweet) {
80 77 return true;
81 78 }
82 79
83   - protected Connection getConnection(Properties prop) {
84   - Connection conn = null;
85   -
86   - try {
87   - System.out.println("Attempting MySQL connection");
88   - conn = DriverManager.getConnection(
89   - "jdbc:mysql://" +
90   - prop.getProperty("hostname") + "/" +
91   - prop.getProperty("database") + "?" +
92   - "user=" + prop.getProperty("username") + "&" +
93   - "password=" + prop.getProperty("password"));
94   -
95   - } catch (SQLException e) {
96   - System.out.println("Failed to connect to MySQL database");
97   - System.exit(3);
98   - }
99   -
100   - return conn;
101   - }
102   -
103 80 /**
104 81 * @param args
105 82 */
@@ -108,16 +85,7 @@ public static void main(String[] args) {
108 85 System.out.println("Starting RouteMe ResponseServer");
109 86 System.out.println("=============================================");
110 87
111   - Properties prop = new Properties();
112   -
113   - try {
114   - prop.load(new FileInputStream("database.properties"));
115   - ResponseServer server = new ResponseServer();
116   - server.start(prop);
117   - } catch (IOException e) {
118   - System.out.println("Failed to load database.properties file");
119   - System.exit(2);
120   - }
121   -
  88 + ResponseServer server = new ResponseServer();
  89 + server.start();
122 90 }
123 91 }
13 RouteMeServer/src/routeme/server/timeline/TimelineServer.java
... ... @@ -0,0 +1,13 @@
  1 +package routeme.server.timeline;
  2 +
  3 +public class TimelineServer {
  4 +
  5 + /**
  6 + * @param args
  7 + */
  8 + public static void main(String[] args) {
  9 + // TODO Auto-generated method stub
  10 +
  11 + }
  12 +
  13 +}

0 comments on commit 2deaed8

Please sign in to comment.
Something went wrong with that request. Please try again.