[英]Something unusual has occurred to cause the driver to fail. Heroku postgresql database
I have created a heroku posgresql database and I am trying to connect my Android app to this database.我已经创建了一个 heroku posgresql 数据库,我正在尝试将我的 Android 应用程序连接到这个数据库。 I am using Android Studio for making the Android app.
我正在使用 Android Studio 来制作 Android 应用程序。 I have used the following code for creating the connection:
我使用以下代码来创建连接:
try {
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection(dbUrl);
}
catch(Exception e){
//handles exception
}
When I execute this code I get the following error: Something unusual has occurred tot cause the driver to fail.当我执行此代码时,我收到以下错误:发生异常情况导致驱动程序失败。 Please report this exception.
请报告此异常。
This is the driver I use: posgresql-42.2.12这是我使用的驱动程序:posgresql-42.2.12
The heroku database has version 12.2 heroku 数据库有版本 12.2
Android studio has java version 1.8 Android工作室有java版本1.8
I am not sure if the driver is in the classpath and I am not sure how to check this.我不确定驱动程序是否在类路径中,也不知道如何检查。 I added the driver to the project this way: app > New > Module > Import.JAR/.AAR Package (here I selected the driver).
我以这种方式将驱动程序添加到项目中:app > New > Module > Import.JAR/.AAR Package(这里我选择了驱动程序)。
To check if the Driver is registered in DriverManager I used this code:要检查驱动程序是否已在 DriverManager 中注册,我使用了以下代码:
Enumeration<Driver> drivers = DriverManager.getDrivers();
drivers.nextElement().getClass().getName();
This gives: org.postgresql.Driver这给出了:org.postgresql.Driver
To check if the Driver understandes the URL I used this code:要检查驱动程序是否理解 URL,我使用了以下代码:
DriverManager.getDriver(dbUrl).getClass().getName();
This also gives org.posgresql.Driver so I think the Driver is registered to the DriverManager and should be able to connect to the database using the URL.这也提供了 org.posgresql.Driver,所以我认为驱动程序已注册到 DriverManager,并且应该能够使用 URL 连接到数据库。 I just really don't know why I keep getting an error when I try to connect with the database.
我真的不知道为什么当我尝试连接数据库时总是出错。
So why does the error occur and how can I resolve the issue?那么为什么会发生错误,我该如何解决这个问题?
My first guess would be a malformed connection string.我的第一个猜测是格式错误的连接字符串。 Are you using a proper JDBC URL (ie it starts with
jdbc:
) for dbUrl
?您是否为 dbUrl 使用了正确的 JDBC URL (即它以
dbUrl
jdbc:
开头)? Also make sure you include sslmode=require
.还要确保包含
sslmode=require
。
In addition, please be aware that the database URL is managed by Heroku and will change under certain circumstances.此外,请注意数据库 URL 由 Heroku 管理,在某些情况下会发生变化。 You can read more about using Heroku Postgres from outside of Heroku in this DevCenter article .
您可以在此 DevCenter 文章中阅读有关从 Heroku 外部使用 Heroku Postgres 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.