public void shutdown() { try { Database.conn.close(); } catch (SQLException e) { LogWriter.write("BusDB:Database : Shutdown with errors.."); LogWriter.write("BusDB:Dataase : Exception: " + e.getMessage()); return; } LogWriter.write("BusDB:Database : Shutting down.."); }
public int addCatalog(int response, String producer) { try { this.statement = Database.conn.createStatement(); String sql = "INSERT INTO catalog (response_time_stamp, producer_ref, time_of_rec) VALUES ( " + "" + response + ", '" + producer + "', 'now')"; this.statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); ResultSet rs = this.statement.getGeneratedKeys(); if (rs.next()) { return rs.getInt(1); } } catch (SQLException e) { LogWriter.write("BusDB:Database : Failed to add new data into catalog table. "); e.printStackTrace(); } return -1; }
@Override public void run() { char c; try { while (running) { char_recieved = telnetHelper.read(); c = (char) char_recieved; c = ansiCoding(c); if (ansiTransmitCode) { if (ansiCode.toString().charAt(ansiCode.toString().length() - 1) == '\n') { telnetHelper.addInputStringFragment(ansiCode.toString(), true); } else { telnetHelper.addInputStringFragment(ansiCode.toString(), false); } ansiCode.delete(0, ansiCode.length()); ansiTransmitCode = false; } if (c == '\r') { // Suppress \r characters } else if (c == '\n') { telnetHelper.addInputStringFragment(java.lang.Character.toString(c), true); } else if (char_recieved > 0) { telnetHelper.addInputStringFragment(java.lang.Character.toString(c), false); } LogWriter.write(EnumLogType.TELNET, java.lang.Character.toString(c)); } } catch (IOException e) { e.printStackTrace(); } }
public void addDataEntry( int id, String lineref, int directionref, String origin, String destination, String longitude, String latitude, String delay, String vehicleref, int djvref, String bearing, String operatorref) { try { this.statement = Database.conn.createStatement(); String sql = "INSERT INTO busdata VALUES ( " + "" + id + "," + "'" + lineref + "', " + directionref + ", " + "'" + origin + "', '" + destination + "', " + "" + longitude + ", " + latitude + ", " + "'" + delay + "', '" + vehicleref + "'," + "" + djvref + ", '" + bearing + "', '" + "" + operatorref + "')"; this.statement.executeUpdate(sql); this.statement.close(); } catch (SQLException e) { LogWriter.write("BusDB:Database : Failed to add new data into busdata table. "); e.printStackTrace(); } }
public void init(String address, String port, String username, String password, String dbname) { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { LogWriter.write("BusDB:Database : No JDBC Driver found..."); e.printStackTrace(); } conn = null; try { conn = DriverManager.getConnection( "jdbc:postgresql://" + address + ":" + Integer.parseInt(port) + "/", username, password); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } catch (SQLException e) { LogWriter.write("BusDB:Database : Connection failed. Check conf.sys for credentials..."); e.printStackTrace(); } if (conn != null) { LogWriter.write("BusDB:Database : Connected to database.."); this.is_init = true; LogWriter.write("BusDB:Database : Initialized successfully."); try { LogWriter.write("BusDB:Database : Creating database for current date."); this.statement = Database.conn.createStatement(); String sql = "CREATE DATABASE db_" + this.currentDate; this.statement.executeUpdate(sql); this.statement.close(); Database.conn.close(); } catch (SQLException e) { LogWriter.write("BusDB:Database : ... database already exists. ( we hope )."); } try { LogWriter.write("BusDB:Database : Connecting to current database."); conn = DriverManager.getConnection( "jdbc:postgresql://" + address + ":" + Integer.parseInt(port) + "/db_" + this.currentDate, username, password); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } catch (SQLException e) { LogWriter.write("BusDB:Database : Connection failed. Something went wrong. "); e.printStackTrace(); } try { this.statement = Database.conn.createStatement(); String sql = "CREATE TABLE catalog (" + "id SERIAL," + "response_time_stamp INTEGER, " + "producer_ref varchar(10)," + "time_of_rec timestamp )"; this.statement.execute(sql); sql = "CREATE TABLE busdata (" + "catalog_id INTEGER NOT NULL, " + "lineref varchar(10) NOT NULL, " + "directionref INTEGER, " + "originname varchar(30), " + "destinationname varchar(30), " + "longtitude double precision, " + "latitude double precision, " + "delay varchar(40), " + "vehicleref varchar(20)," + "dvjref INTEGER," + "bearing double precision," + "operatorref varchar(30) )"; this.statement.execute(sql); this.statement.close(); LogWriter.write("BusDB:Database : Created tables for database."); } catch (SQLException e) { LogWriter.write("BusDB:Database : Failed to create tables for selected database"); } } else LogWriter.write("BusDB:Database : Failed to make connection.."); }