Search code examples
javasqlpostgresqlherokusqlexception

'java.sql.SQLException: No suitable driver fround' [SQLException, Heroku, Postgresql]


I am currently trying to deploy an old project of mine to heroku. Locally everything work perfect. (also with the amazonaws psql database provided by heroku). But as soon as I try to deploy the application to heroku, I get this (see below) error in the heroku console.

Note: some letters are replaced with 'x'

java.sql.SQLException: No suitable driver found for jdbc:postgresql://xxx-xx-xx-xxx-xxx.eu-west-1.compute.amazonaws.com:5432/dxxxx5xx6xxxxx

enter image description here

To connect the Java Application to the postgresql database I do:

     String jdbcURL = System.getenv("DATABASE_SERVER");
            String username = System.getenv("DATABASE_USERNAME");
            String password = System.getenv("DATABASE_PASSWORD");

            try {
                connection = DriverManager.getConnection(jdbcURL, username, password);

                System.out.println("Verbindung zur Datenbank hergestellt");

                statement = connection.createStatement();

            } catch (SQLException e) {
                e.printStackTrace();
            }

Full code can be seen here:

https://github.com/ConfusingBot/bot/blob/master/src/de/confusingbot/manage/sql/SQLManager.java

The env variables are defined in heroku and local. And local everything works fine with the same variables. enter image description here

pom.xml: https://github.com/ConfusingBot/bot/blob/master/pom.xml

Thank you

Edited: I found out that the postgres dependency does exist in heroku.. (see image below) But soemhow the driver can't be found.. enter image description here Heroku cannot find Postgres JDBC Driver

But unfortunately in my case their fix doesn't work :/


Solution

  • Building the jar which includes all dependencies works quite fine.. for that we have to define a plugin in the pom.xml and thats it.. (see below)

    <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>de.confusingbot.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>
    

    Thanks for the hint: https://stackoverflow.com/a/56800302/14108895