import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
import java.sql.*;
import org.apache.commons.dbcp2.*;

public class Main extends HttpServlet {

  private BasicDataSource connectionPool;

  public Main() throws URISyntaxException, SQLException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();

    connectionPool = new BasicDataSource();
    connectionPool.setDriverClassName("org.postgresql.Driver");
    connectionPool.setUrl(dbUrl);
    connectionPool.setUsername(username);
    connectionPool.setPassword(password);
    connectionPool.setInitialSize(1);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    if (req.getRequestURI().endsWith("/db")) {
      showDatabase(req,resp);
    } else {
      showHome(req,resp);
    }
  }

  private void showHome(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.getWriter().print("Hello from Java!");
  }

  private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    try {
      Connection connection = connectionPool.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");

      String out = "Hello!\n";
      while (rs.next()) {
          out += "Read from DB: " + rs.getTimestamp("tick") + "\n";
      }

      resp.getWriter().print(out);
    } catch (Exception e) {
      resp.getWriter().print("There was an error: " + e.getMessage());
    }
  }

  public static void main(String[] args) throws Exception{
    Server server = new Server(Integer.valueOf(System.getenv("PORT")));
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    context.addServlet(new ServletHolder(new Main()),"/*");
    server.start();
    server.join();
  }
}

@@ -497,123 +497,8 @@
 %22));
-%0A%0A    String username = dbUri.getUserInfo().split(%22:%22)%5B0%5D;%0A    String password = dbUri.getUserInfo().split(%22:%22)%5B1%5D;
 %0A   
@@ -567,25 +567,24 @@
 .getPath();%0A
-%0A
     connecti
@@ -614,21 +614,63 @@
 ource();
+%0A%0A    if (dbUri.getUserInfo() != null) %7B
 %0A    
+  
 connecti
@@ -683,50 +683,55 @@
 .set
-DriverClassName(%22org.postgresql.Driver%22
+Username(dbUri.getUserInfo().split(%22:%22)%5B0%5D
 );%0A
+  
     
@@ -752,20 +752,59 @@
 .set
-Url(dbUrl
+Password(dbUri.getUserInfo().split(%22:%22)%5B1%5D
 );%0A
+    %7D%0A
     
@@ -825,25 +825,47 @@
 .set
-Username(username
+DriverClassName(%22org.postgresql.Driver%22
 );%0A 
@@ -889,25 +889,17 @@
 .set
-Password(password
+Url(dbUrl
 );%0A 

improve the parsing of dbUri to handle missing username and password