Exemplo n.º 1
0
  /**
   * Updates the table of quotes for this symbol. Assumes that the listofsymbols has been updated,
   * but the table itself may not exist. Takes a date range, including both start and end days.
   *
   * <p>Yahoo Finance returns an error message rather than an empty CSV if the start and end dates
   * are today. The caller is responsible for checking that the call range is acceptable.
   *
   * @param symbol - symbol to update
   * @param startDate - beginning of range to add to
   * @param endDate - end of range to add to
   */
  static void updateSymbol(String symbol, Date startDate, Date endDate) throws Exception {
    System.out.println("Trying to update:" + symbol);
    Connection conn = initialize();
    Statement stat = conn.createStatement();
    URL data = YahooCsvDownloadUrl(symbol, startDate, endDate);
    BufferedReader in = null;
    try {
      in = new BufferedReader(new InputStreamReader(data.openStream()));
    } catch (java.io.FileNotFoundException e) {
      System.out.println("Symbol not found:" + symbol);
      e.printStackTrace();
      return;
    }
    CsvReader reader = new CsvReader(in);
    reader.readHeaders();
    String[] headers = reader.getHeaders();
    stat.executeUpdate("CREATE TABLE IF NOT EXISTS " + symbol + " (" + getColNames(headers) + ");");
    String statement =
        "INSERT INTO "
            + symbol
            + " ("
            + getColNames(headers)
            + ") VALUES ("
            + getQueryQuestionMarks(headers)
            + ");";

    PreparedStatement prep = conn.prepareStatement(statement);

    while (reader.readRecord()) {
      for (int j = 0; j < headers.length; j++) {
        String str = reader.get(headers[j]);
        prep.setString(j + 1, str);
      }
      // TODO: salim, what's the point of these calls?
      prep.addBatch();
      conn.setAutoCommit(false);
      prep.executeBatch();
      conn.setAutoCommit(true);
    }
    reader.close();
    in.close();
    conn.close();
  }