public static void main(String[] args) throws SQLException { // open database Connection conn = DriverManager.getConnection("jdbc:h2:./main"); createTables(conn); // serve external files Spark.externalStaticFileLocation("public"); Spark.init(); // insert test data if (selectCountries(conn).size() == 0) { insertCountry(conn, "United States", "US"); insertCountry(conn, "Canada", "CA"); insertCountry(conn, "Mexico", "MX"); } // create routes for AJAX Spark.get( "/get-countries", ((request, response) -> { JsonSerializer serializer = new JsonSerializer(); String json = serializer.serialize(selectCountries(conn)); return json; })); Spark.get( "/get-country", ((request, response) -> { String id = request.queryParams("id"); try { int idNum = Integer.valueOf(id); JsonSerializer serializer = new JsonSerializer(); String json = serializer.serialize(selectCountry(conn, idNum)); return json; } catch (Exception e) { } return ""; })); Spark.post( "/add-country", ((request, response) -> { String name = request.queryParams("name"); String abbrev = request.queryParams("abbrev"); if (name == null || abbrev == null) { Spark.halt(403); } insertCountry(conn, name, abbrev); return ""; })); }
static void saveCountry() { File f = new File( "save.json"); // json library wants you to set getters for anything you want to save JsonSerializer serializer = new JsonSerializer(); String contentToSave = serializer.serialize(country); try { FileWriter fw = new FileWriter(f); fw.write(contentToSave); fw.close(); } catch (Exception e) { System.out.println("Save not successful."); } }