import java.sql.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;

import java.net.URI;
import java.net.URISyntaxException;

import static spark.Spark.*;
import spark.template.freemarker.FreeMarkerEngine;
import spark.ModelAndView;
import static spark.Spark.get;

import com.heroku.sdk.jdbc.DatabaseUrl;

public class Main {

  public static void main(String[] args) {

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    get("/hello", (req, res) -> "Hello World");

    /**get("/", (request, response) -> {
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("message", "Hello World!");

            return new ModelAndView(attributes, "index.ftl");
        }, new FreeMarkerEngine());**/

    get("/db", (req, res) -> {
      Connection connection = null;
      Map<String, Object> attributes = new HashMap<>();
      try {
        connection = DatabaseUrl.extract().getConnection();

        Statement stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
        stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
        ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

        ArrayList<String> output = new ArrayList<String>();
        while (rs.next()) {
          output.add( "Read from DB: " + rs.getTimestamp("tick"));
        }

        attributes.put("results", output);
        return new ModelAndView(attributes, "db.ftl");
      } catch (Exception e) {
        attributes.put("message", "There was an error: " + e);
        return new ModelAndView(attributes, "error.ftl");
      } finally {
        if (connection != null) try{connection.close();} catch(SQLException e){}
      }
    }, new FreeMarkerEngine());

  }

}

@@ -788,20 +788,16 @@
 ));**/%0A%0A
-    
 get(%22/db
@@ -1039,84 +1039,233 @@
 ();%0A
-        stmt.executeUpdate(%22CREATE TABLE IF NOT EXISTS ticks (tick timestamp
+%09%09stmt.executeUpdate(%22CREATE TABLE IF NOT EXISTS Login (id int, username varchar(50),passwd varchar(50)%22);%0A        stmt.executeUpdate(%22CREATE TABLE IF NOT EXISTS Comments_cmu (id int, username varchar(50),comments varchar(500
 )%22);
@@ -1265,32 +1265,34 @@
 500)%22);%0A        
+//
 stmt.executeUpda
@@ -1327,24 +1327,27 @@
  (now())%22);%0A
+%09%09%0A
         Resu
@@ -1387,23 +1387,27 @@
 ECT 
-tick
+*
  FROM 
-ticks
+Comments_cmu
 %22);%0A
@@ -1553,20 +1553,148 @@
 estamp(%22
-tick
+id%22));%0A%09%09  output.add( %22Read from DB: %22 + rs.getTimestamp(%22username%22));%0A%09%09  output.add( %22Read from DB: %22 + rs.getTimestamp(%22comments
 %22));%0A   

create database