1111import spark .ModelAndView ;
1212import static spark .Spark .get ;
1313
14+ import com .heroku .sdk .jdbc .DatabaseUrl ;
15+
1416public class Main {
1517
1618 public static void main (String [] args ) {
@@ -31,7 +33,7 @@ public static void main(String[] args) {
3133 Connection connection = null ;
3234 Map <String , Object > attributes = new HashMap <>();
3335 try {
34- connection = getConnection ();
36+ connection = DatabaseUrl . extract (). getConnection ();
3537
3638 Statement stmt = connection .createStatement ();
3739 stmt .executeUpdate ("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)" );
@@ -51,22 +53,8 @@ public static void main(String[] args) {
5153 } finally {
5254 if (connection != null ) try {connection .close ();} catch (SQLException e ){}
5355 }
54- }, new FreeMarkerEngine ());
55-
56- }
57-
58- private static Connection getConnection () throws URISyntaxException , SQLException {
59- URI dbUri = new URI (System .getenv ("DATABASE_URL" ));
60- int port = dbUri .getPort ();
61- String dbUrl = "jdbc:postgresql://" + dbUri .getHost () + ":" + port + dbUri .getPath ();
56+ }, new FreeMarkerEngine ());
6257
63- if (dbUri .getUserInfo () != null ) {
64- String username = dbUri .getUserInfo ().split (":" )[0 ];
65- String password = dbUri .getUserInfo ().split (":" )[1 ];
66- return DriverManager .getConnection (dbUrl , username , password );
67- } else {
68- return DriverManager .getConnection (dbUrl );
69- }
7058 }
7159
7260}
0 commit comments