1

I have craeted a Web Application that can be deployed on Heroku by maven eclipse.

Group Id: org.glassfish.jersey.archetypes

Artifact Id: jersey-heroku-webapp

version: 2.17

then I followed this guide(1.5) to push it to Heroku.

https://jersey.java.net/documentation/latest/getting-started.html#heroku-webapp

I can not access my apple class from the http link- like this:

https://salty-refuge-2027.herokuapp.com/apple

and I am getting the error 500

I have tested it before without the JDBC procedure and I got the output Hello, from apple class therefore I guess it depends on the my implementation for the Heroku Postgres https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java

    import java.net.URI;
    import java.net.URISyntaxException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("apple")
    public class Apple {

            @GET
            @Produces(MediaType.TEXT_PLAIN)
            public String getIt() {
                try {
                    getConnection();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return "Hello, from apple class";
            }

            private static Connection getConnection() throws URISyntaxException, SQLException {
        //I have modified my orginal ingredients.
                URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@
ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

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

                Connection con = DriverManager.getConnection(dbUrl, username, password);
                System.out.println("It works");
                return con;
            }
        }

I appreciate any help.

MrPencil
  • 934
  • 8
  • 17
  • 36
  • The host string contains hostname of a mysql server? and by the way, at least post a workable https://sky-sun-6789.herokuapp.com/ link – cctan May 13 '15 at 09:07
  • @cctan: their host string contains just this `ec6-70-19-289-719.sun-1.amazonaws.com` – MrPencil May 13 '15 at 09:08
  • @cctan: this link `sky-sun-6789.herokuapp.com` is not the right one it is just an example – MrPencil May 13 '15 at 09:09
  • I have changed the host string to `ec6-70-19-289-719.sun-1.amazonaws.com/database` It does not work too also without `jdbc:mysql://` – MrPencil May 13 '15 at 09:14
  • I have tried it with `jdbc:ec6-70-19-289-719.sun-1.amazonaws.com/database` and It does not still work. Do I have to configure my database before I try to call the link somewhere on Heroku? – MrPencil May 13 '15 at 09:22
  • does this [help](https://devcenter.heroku.com/articles/heroku-postgresql#jdbc) or [this](http://stackoverflow.com/questions/15191259/how-to-deploy-local-mysql-database-to-heroku)? – cctan May 13 '15 at 09:23
  • No it does not still work. – MrPencil May 13 '15 at 09:26

1 Answers1

0

You should call System.getenv("DATABASE_URL"). You are calling getenv with the value of the env var, which will return null.

codefinger
  • 10,088
  • 7
  • 39
  • 51