3

I'm trying to connect my web app deployed in Heroku with the postgre database it offers.

I followed the instructions given at this site and deployed my app, but it threw the following error message

No suitable driver found for jdbc:postgresql://ec2-184-73-222-90.compute-1.amazonaws.com:5432/d9l4hq0c2h46o6?user=wyhgmwybghpndl&password=kWxLQ_y2risoVSFbzTKR_YsUFF&sslmode=require

I tried the suggestions given here and here, but didn't work. I have ALSO added postgresql-9.4.1211.jre6.jar as External JARS in my buildpath.

Following is my Test.java that receives a GET request and tries to connect to Heroku database

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("wallpost")
public class wallpost {

    private static String connectionMsg = null;
    private static final String DATABASE_URL = "DATABASE_URL";
    private static final String JDBC_POSTGRE = "jdbc:postgresql://" ;
    private static final String JDBC_DATABASE_URL = "JDBC_DATABASE_URL"; 

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public  String getPosts() {

        Connection conn = null;
        try {
            conn = tryAndConnectToDB();
        } catch (SQLException e) {
            connectionMsg = e.getMessage();
        } catch (URISyntaxException e) {
            connectionMsg = e.getMessage();
            e.printStackTrace();
        }

        if  (conn != null)
            try {
                return "Oops!! No posts made yet!!" + conn.getSchema();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connectionMsg;
    }

    private static Connection tryAndConnectToDB() throws URISyntaxException, SQLException {

        try {
            Class.forName("org.postgresql.Driver");
        } 
        catch (ClassNotFoundException e) {

            connectionMsg = e.getMessage();         
        }

        final String dbUrl = System.getenv(JDBC_DATABASE_URL);

            //URI dbURI = new URI(System.getenv(DATABASE_URL));

//          final String userName = dbURI.getUserInfo().split(":")[0];
//          final String userPassword = dbURI.getUserInfo().split(":")[1];
//          
//          final String dbURL = JDBC_POSTGRE + dbURI.getHost() + ":" + dbURI.getPort() + dbURI.getPath();

        //  return DriverManager.getConnection(dbURL,userName,userPassword);

        return DriverManager.getConnection(dbUrl);


        }

    }

Thank you for your time!

Community
  • 1
  • 1
Auro
  • 1,578
  • 3
  • 31
  • 62

1 Answers1

1

First of all, you need to reset your database credentials because you've posted them publicly. Run this command:

heroku pg:credentials DATABASE --reset

That error message suggests that you do not have the Postgres JDBC driver on your classpath.

The postgresql-9.4.1211.jre6.jar almost certainly not the JAR you want. It's for Java 6. And you don't want to make it an external dependency.

In your pom.xml, you should have this:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.4-1211</version>
</dependency>
codefinger
  • 10,088
  • 7
  • 39
  • 51