伙计们,我有一个带有PostgreSQL数据库的heroku Web应用程序。如您在图片中所见,DATABASE_URL
已设置:
但是由于某些原因,当我尝试访问环境变量以连接到数据库时,我得到了NullPointerException
,因为显然DATABASE_URL
不存在:
private static Connection getConnection() throws URISyntaxException, SQLException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
return DriverManager.getConnection(dbUrl, username, password);
}
这是怎么回事?我该如何解决?
答案 0 :(得分:1)
Heroku配置变量为environment variables。它们是在特定环境中定义的,并适用于特定环境,在本例中为应用程序在Heroku上运行的环境。它们不会影响其他环境,例如,位于您前面的机器上的环境。
您可以在本地configure NetBeans to use an environment variable for your project。 with banned_usrs as (select users_id from users where banned = 'Yes')
select
request_at as Day,
cast(sum(iif(status like 'cancelled%', 1.0, 0.0))/count(status) as decimal(4,2)) as [Cancellation Rate]
from trips
where
request_at between '2013-10-01' and '2013-10-03' and
client_id not in (select users_id from banned_usrs) and
driver_id not in (select users_id from banned_usrs);
group by
request_at;
的值可以与Heroku上的值不同,只需确保将其设置为有效的PostgreSQL URL值即可。
答案 1 :(得分:0)
对于Windows
write set DATABASE_URL=url address from Heroku
对于macOS
export DATABASE_URL=url address from Heroku
确保首先安装了PostgreSQL。