gpt4 book ai didi

java - 我无法连接 Heroku 上的数据库

转载 作者:行者123 更新时间:2023-12-01 11:33:43 24 4
gpt4 key购买 nike

我创建了一个 Web 应用程序,可以通过 maven eclipse 部署在 Heroku 上。

组 ID: org.glassfish.jersey.archetypes

工件 ID: jersey-heroku-webapp

版本:2.17

然后我按照这个指南(1.5)将其推送到 Heroku。

https://jersey.java.net/documentation/latest/getting-started.html#heroku-webapp

我无法从 http 链接访问我的 apple 类,如下所示:

https://salty-refuge-2027.herokuapp.com/apple

我收到错误500

我之前已经在没有 JDBC 过程的情况下测试过它,并且得到了输出 Hello, from apple class 因此我想这取决于我对 Heroku Postgres 的实现 https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java

    import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("apple")
public class Apple {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
try {
getConnection();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "Hello, from apple class";
}

private static Connection getConnection() throws URISyntaxException, SQLException {
//I have modified my orginal ingredients.
URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@
ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
+ dbUri.getPort() + dbUri.getPath();

Connection con = DriverManager.getConnection(dbUrl, username, password);
System.out.println("It works");
return con;
}
}

Heroku 日志:

D:\maven\heroku_maven_by_eclipse\serverSide>heroku logs
2015-05-13T11:19:11.364893+00:00 app[web.1]: at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:401)
2015-05-13T11:19:11.364825+00:00 app[web.1]: at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:11.364826+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:11.364828+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:11.364895+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:11.364896+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)
2015-05-13T11:19:11.364846+00:00 app[web.1]: java.lang.NullPointerException
2015-05-13T11:19:11.364901+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:11.364898+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:11.364900+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:11.364902+00:00 app[web.1]: at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:11.364904+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:11.364908+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:11.364906+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:11.364909+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:11.364805+00:00 app[web.1]: 2015-05-13 11:19:11.364:WARN:oejs.S
ervletHandler:qtp688970022-21:
2015-05-13T11:19:11.364910+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:11.364911+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:11.364913+00:00 app[web.1]: at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:11.364916+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:11.364914+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:11.364917+00:00 app[web.1]: at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:11.364905+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:11.364938+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:11.364941+00:00 app[web.1]: at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:11.364940+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:11.366434+00:00 heroku[router]: at=info method=GET path="/apple
" host=salty-refuge-2027.herokuapp.com request_id=77099e48-00b0-4ac2-8c41-c631b9
619a63 fwd="31.17.41.138" dyno=web.1 connect=1ms service=5ms status=500 bytes=50
5
2015-05-13T11:19:12.339340+00:00 heroku[router]: at=info method=GET path="/apple
" host=salty-refuge-2027.herokuapp.com request_id=cb3396d8-474c-436c-9f13-a6d7a3
ac102e fwd="31.17.41.138" dyno=web.1 connect=1ms service=6ms status=500 bytes=50
5
2015-05-13T11:19:12.337223+00:00 app[web.1]: 2015-05-13 11:19:12.336:WARN:oejs.S
ervletHandler:qtp688970022-22:
2015-05-13T11:19:12.337244+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:222)
2015-05-13T11:19:12.337246+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:12.337248+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:12.337235+00:00 app[web.1]: javax.servlet.ServletException: jav
a.lang.NullPointerException
2015-05-13T11:19:12.337237+00:00 app[web.1]: at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:421)
2015-05-13T11:19:12.337240+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:12.337242+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)
2015-05-13T11:19:12.337259+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:12.337250+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:12.337252+00:00 app[web.1]: at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:12.337254+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:12.337256+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:12.337257+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:12.337261+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:12.337263+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:12.337265+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:12.337271+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:12.337272+00:00 app[web.1]: at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:12.337274+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:12.337275+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:12.337277+00:00 app[web.1]: at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:12.337278+00:00 app[web.1]: Caused by:
2015-05-13T11:19:12.337269+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:12.337267+00:00 app[web.1]: at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:12.337280+00:00 app[web.1]: java.lang.NullPointerException
2015-05-13T11:19:12.337281+00:00 app[web.1]: at java.net.URI$Parser.parse(URI
.java:3023)
2015-05-13T11:19:12.337293+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvo
cationHandlerFactory.java:81)
2015-05-13T11:19:12.337283+00:00 app[web.1]: at java.net.URI.<init>(URI.java:
595)
2015-05-13T11:19:12.337284+00:00 app[web.1]: at busTracker.serverSide.Apple.g
etConnection(Apple.java:34)
2015-05-13T11:19:12.337285+00:00 app[web.1]: at busTracker.serverSide.Apple.g
etIt(Apple.java:21)
2015-05-13T11:19:12.337290+00:00 app[web.1]: at sun.reflect.DelegatingMethodA
ccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2015-05-13T11:19:12.337295+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMet
hodDispatcher.java:164)
2015-05-13T11:19:12.337296+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMe
thodDispatcher.java:181)
2015-05-13T11:19:12.337287+00:00 app[web.1]: at sun.reflect.NativeMethodAcces
sorImpl.invoke0(Native Method)
2015-05-13T11:19:12.337288+00:00 app[web.1]: at sun.reflect.NativeMethodAcces
sorImpl.invoke(NativeMethodAccessorImpl.java:57)
2015-05-13T11:19:12.337291+00:00 app[web.1]: at java.lang.reflect.Method.invo
ke(Method.java:606)
2015-05-13T11:19:12.337300+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResource
MethodDispatcher.java:101)
2015-05-13T11:19:12.337308+00:00 app[web.1]: at org.glassfish.jersey.internal
.Errors$1.call(Errors.java:267)
2015-05-13T11:19:12.337310+00:00 app[web.1]: at org.glassfish.jersey.internal
.Errors.process(Errors.java:315)
2015-05-13T11:19:12.337311+00:00 app[web.1]: at org.glassfish.jersey.internal
.Errors.process(Errors.java:297)
2015-05-13T11:19:12.337301+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
2015-05-13T11:19:12.337303+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
2015-05-13T11:19:12.337304+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
2015-05-13T11:19:12.337306+00:00 app[web.1]: at org.glassfish.jersey.server.S
erverRuntime$2.run(ServerRuntime.java:305)
2015-05-13T11:19:12.337307+00:00 app[web.1]: at org.glassfish.jersey.internal
.Errors$1.call(Errors.java:271)
2015-05-13T11:19:12.337298+00:00 app[web.1]: at org.glassfish.jersey.server.m
odel.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(Jav
aResourceMethodDispatcherProvider.java:203)
2015-05-13T11:19:12.337330+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:12.337332+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:12.337326+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:12.337312+00:00 app[web.1]: at org.glassfish.jersey.internal
.Errors.process(Errors.java:267)
2015-05-13T11:19:12.337314+00:00 app[web.1]: at org.glassfish.jersey.process.
internal.RequestScope.runInScope(RequestScope.java:317)
2015-05-13T11:19:12.337337+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:12.337333+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:12.337334+00:00 app[web.1]: at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:12.337336+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:12.337315+00:00 app[web.1]: at org.glassfish.jersey.server.S
erverRuntime.process(ServerRuntime.java:288)
2015-05-13T11:19:12.337325+00:00 app[web.1]: at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:401)
2015-05-13T11:19:12.337339+00:00 app[web.1]: at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:12.337316+00:00 app[web.1]: at org.glassfish.jersey.server.A
pplicationHandler.handle(ApplicationHandler.java:1110)
2015-05-13T11:19:12.337329+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:222)
2015-05-13T11:19:12.337341+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:12.337338+00:00 app[web.1]: at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:12.337342+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:12.337344+00:00 app[web.1]: at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:12.337343+00:00 app[web.1]: at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:12.337346+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:12.337350+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:12.337353+00:00 app[web.1]: at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:12.337349+00:00 app[web.1]: at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:12.337347+00:00 app[web.1]: at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:12.337351+00:00 app[web.1]: at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:12.337328+00:00 app[web.1]: at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)

最佳答案

嗯,从日志来看。看来错误是这部分

URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

dbUri 变量为 null,因为应用程序找不到系统环境变量。也许这就是你的意思?

URI dbUri = new URI("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9");

如果没有,您可能需要检查系统环境变量中是否有此 key postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9 虽然我认为拥有一个带有这样的键的环境变量是很荒谬的:p

编辑

来自文档。看来你的代码应该这样写

 URI dbUri = new URI(System.getenv("DATABASE_URL"));

“DATABASE_URL” 是一个已设置到您的环境中的变量,因此它应该检索正确的值。你可能想在Linux系统上像这样检查它

echo $DATABASE_URL

或在 Windows 中

echo %DATABASE_URL%

关于java - 我无法连接 Heroku 上的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com