'java.sql.SQLException: 找不到合适的驱动程序' [SQLException, Heroku, Postgresql]

问题描述

我目前正在尝试将我的一个旧项目部署到 heroku。 在本地一切正常。 (还有由 heroku 提供的 amazonaws psql 数据库)。 但是,一旦我尝试将应用程序部署到 heroku,我就会在 heroku 控制台中收到此(见下文)错误

注意:有些字母用'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

为了将 Java 应用程序连接到 postgresql 数据库,我这样做:

     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();
            }

完整代码可以在这里看到:

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

env 变量在 heroku 和 local 中定义。本地一切正常,使用相同的变量。

enter image description here

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

谢谢

编辑: 我发现heroku中确实存在postgres依赖项..(见下图)但是找不到驱动程序..

enter image description here

Heroku cannot find Postgres JDBC Driver

但不幸的是,在我的情况下,他们的修复不起作用:/

解决方法

构建包含所有依赖项的 jar 工作得很好.. 为此,我们必须在 pom.xml 中定义一个插件,仅此而已..(见下文)

<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>

感谢提示:https://stackoverflow.com/a/56800302/14108895

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...