Connecting Elephantsql with java

Asked

Viewed 521 times

1

I’m trying to make a connection with the Elephantsql (postgres) through java. With the same code I was able to connect to the local postgres (just changing the url, user, password). I’m trying to do it like this:

private static final String DRIVER = "org.postgresql.Driver";
private static final String URL = "jdbc:postgres://nklbcxow:[email protected]:5432/nklbcxow";
private static final String USER = "nklbcxow";
private static final String PASS = "MINHA_SENHA_NO_ELEPHANTSQL";

public static Connection getConnection() throws SQLException {
    try {
        Class.forName(DRIVER);

        return DriverManager.getConnection(URL, USER, PASS);
    } catch (ClassNotFoundException ex) {
        throw new RuntimeException("Erro na conexão", ex);
    }
}

But there’s a driver error:

Exception in thread "main" java.sql.Sqlexception: No suitable driver found for jdbc:postgres://nklbcxow:[email protected]:5432/nklbcxow at java.sql.Drivermanager.getConnection(Drivermanager.java:689) at java.sql.Drivermanager.getConnection(Drivermanager.java:247) At Connectionfactory.getConnection(Connectionfactory.java:20) At productodao.registerTemp(Productodao.java:20) At Bdlearning.main(Bdlearning.java:25) C: Users Mathe Appdata Local Netbeans Cache 8.2 executor-snippets run.xml:53: Java returned: 1 CONSTRUCTION FAILURE (total time: 0 seconds)

Being that the site itself, is indicating to use the driver I’m already using : inserir a descrição da imagem aqui

I believe it is some format in the url that I’m not aware of, I tried some other ways, but no function.

Their website has a doc that gives an example of how to do in java, I copied the example and continued giving problem

2 answers

1

I believe your connection URL is not correct. Try this:

Substitute ENDERECO, PORTA, NOME_DO_BANCO, SEU_USUARIO and SUA_SENHA at the right values.

private static final String DRIVER = "org.postgresql.Driver";
private static final String URL = "jdbc:postgres://ENDERECO:PORTA/NOME_DO_BANCO";
private static final String USER = "SEU_USUARIO";
private static final String PASS = "SUA_SENHA";
  • So, friend, these are the information Elephant gives me. The url he gives me has its own format

  • @Matheus maybe your Postgresql driver . jar is not configured correctly in your classpath.

  • How do I see it ? As I said before, I used this same drive to connect to a local Postgressql.

  • And in local Postgresql worked? You just changed the connection parameters?

  • Yeah, it worked just by changing the parameters

1

I know the question is almost 3 years since it was asked, but how have I been through this problem recently, and this thread helped me, I would like to help who else, due to doubts, also want to use Elequantsql as a database and integrate with Java.

As stated in the Elequantsql documentation for Java (https://www.elephantsql.com/docs/java.html), the format of the URL should be as follows:

 String url = "jdbc:postgresql://host:port/database";

Where host is just the outside of the field parentheses Server, located in the tab Details of the bank instance created. Still, NAY the same URL format as indicated in the tab Details of the database when it is used remotely by some Java application, but the one pointed above.

Finally, although the answer given above helped me in connecting to Elequantsql’s remote Postgresql database, it contains a small misconception in its URL, which you should use postgresql, and not postgres, in its formatting, thus getting the default configuration of the parameters passed to the method getConnection() class Drivermanager when connecting the Java app to the database:

private static final String DRIVER = "org.postgresql.Driver";
private static final String URL = "jdbc:postgresql://ENDERECO:PORTA/NOME_DO_BANCO";
private static final String USER = "SEU_USUARIO";
private static final String PASS = "SUA_SENHA";

DriverManager.getConnection(URL, USER, PASS);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.