如何从 Glassfish 连接到外部 MySQL 服务器
我正在尝试连接到远程 MySQL 服务器。我已成功从 Eclipse(数据源视图)建立连接,现在可以浏览数据库的内容。
但现在我希望能够在我的 Java EE 6 应用程序中使用该数据库,但它不起作用。我尝试过的操作:
在管理面板中添加了用户名、密码、服务器名称、URL、端口作为属性。
这是我使用的 URL:jdbc:mysql://MYHOST:3306/MYDATABASE 可能没有任何问题。我还将我的 IP 添加到 CPanel 中的远程列表中,以便我可以远程连接。我已经下载了这个驱动程序: http: //www.java.net/external?url=http://dev.mysql.com/downloads/connector/j/ 并将其添加到domains/domain1/lib文件夹中。
我还看到另外 4 个文件夹;数据库、ext、applibs 和类,因此我将驱动程序添加到这些文件夹中以确保确定。这是错误的吗?我是否需要将驱动程序添加到 Eclipse 项目中的某个文件夹才能使其正常工作?
资源类型:javax.sql.DataSource 数据源类名:com.mysql.jdbc.jdbc2.可选.MysqlConnectionPoolDataSource
UPDATE
HTTP 状态 500 -
类型异常报告
消息
说明服务器遇到内部错误 (),导致无法执行 它无法满足此请求。
异常
javax.servlet.ServletException:WELD-000049 无法调用 [方法] @PostConstruct 公共 no.breakpoint.sertifikat.interfaces.QuestionController.intialize() 上 no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d
根本原因
org.jboss.weld.exceptions.WeldException: WELD-000049 无法调用 [方法] @PostConstruct 公共 no.breakpoint.sertifikat.interfaces.QuestionController.intialize() 上 no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d
根本原因
java.lang.reflect.InitationTargetException
根本原因
javax.ejb.EJBException
根本原因
java.lang.NullPointerException
注意异常的完整堆栈跟踪及其根本原因是 可在 GlassFish Server 开源版 3.1.1 日志中找到。 GlassFish Server 开源版 3.1.1
EJB:
@Override
public List<Question> all() {
// List<Question> list = new ArrayList<Question>();
// Question question = new Question();
// question.setDescription("SDfasfasdfasf");
// question.setId(32);
// question.setImage("sdafasdf");
// list.add(question);
//
// return list;
//
return questionDao.all();
}
JSF bean
@Named
@RequestScoped
public class QuestionController {
@EJB
private QuestionLocalBusiness questionManager;
private List<Question> questions;
@PostConstruct
public void intialize() {
questions = questionManager.all();
}
public void setQuestions(List<Question> list) {
questions = list;
}
public List<Question> getQuestions() {
return questions;
}
}
错误日志:
WARNING: StandardWrapperValve[FacesServlet]: PWC1406: Servlet.service() for servlet FacesServlet threw exception org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @PostConstruct public com.bpoint.interfaces.QuestionController.intialize() on com.bpoint.interfaces.QuestionController@10614f3d at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:595) at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.postConstruct(ManagedBean.java:200) at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:340) at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:121) at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99) at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87) at no.breakpoint.sertifikat.interfaces.org$jboss$weld$bean-SertifikatWeb_war-ManagedBean-class_no$breakpoint$sertifikat$interfaces$QuestionController_$$_WeldClientProxy.getQuestions(org$jboss$weld$bean-SertifikatWeb_war-ManagedBean-class_no$breakpoint$sertifikat$interfaces$QuestionController_$$_WeldClientProxy.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at javax.el.BeanELResolver.getValue(BeanELResolver.java:302) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) at com.sun.el.parser.AstValue.getValue(AstValue.java:116) at com.sun.el.parser.AstValue.getValue(AstValue.java:163) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219) at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) at javax.faces.component.UIData.getValue(UIData.java:731) at javax.faces.component.UIData.getDataModel(UIData.java:1798) at javax.faces.component.UIData.getRowCount(UIData.java:356) at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:456) at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:201) at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:180) at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763) at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:305) at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54) at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163) at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:299) at org.jboss.weld.introspector.jlr.WeldMethodImpl.invoke(WeldMethodImpl.java:193) at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:591) ... 66 more Caused by: javax.ejb.EJBException at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) at $Proxy231.all(Unknown Source) at no.breakpoint.sertifikat.interfaces.QuestionController.intialize(QuestionController.java:24) ... 76 more Caused by: java.lang.NullPointerException at no.breakpoint.sertifikat.application.QuestionManagerEJB.all(QuestionManagerEJB.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) ... 79 more
DAO:
package no.breakpoint.sertifikat.infrastructure;
import java.util.List;
@NamedQuery(name = "allQuestions", query = "SELECT q FROM Question q")
public class DefaultQuestionDao implements QuestionDao {
@PersistenceContext(unitName = "MyDatabasePU")
private EntityManager entityManager;
@Override
public void create(Question question) {
entityManager.persist(question);
}
@Override
public void delete(Question question) {
entityManager.remove(question);
}
@Override
public void update(Question question) {
entityManager.merge(question);
}
@Override
public Question read(Integer id) {
return entityManager.find(Question.class, id);
}
@Override
public List<Question> all() {
TypedQuery<Question> query = entityManager.createNamedQuery("allQuestions", Question.class);
return query.getResultList();
}
}
如果我取消注释我现在注释的内容,我使用的数据表将正常工作,500 错误就会消失。但是当我尝试从数据库中获取它时,它给了我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,
EntityManager
是null
?@PersistenceContext
将只能注入到容器/框架托管类中,例如@Stateless
EJB,CDI@Named
bean,一个 JSF@ManagedBean
bean 等。但是,您的DefaultQuestionDao
似乎根本不受管理。鉴于到目前为止的设置,我将开始将您的
DefaultQuestionDao
标记为@Stateless
EJB。整个设置充其量是令人困惑的,堆栈跟踪告诉我在代码片段中没有看到的QuestionManagerEJB
,只有一个DefaultQuestionDao
和一个QuestionBusinessLocal
。也许您还需要将DefaultQuestionDao
作为@EJB
注入QuestionManagerEJB
中。此外,还有另一个问题:
@NamedQuery
必须在@Entity
类或其超类之一,否则将找不到。So, the
EntityManager
isnull
? The@PersistenceContext
will only be injected in container/framework managed classes such as a@Stateless
EJB, a CDI@Named
bean, a JSF@ManagedBean
bean, etc. YourDefaultQuestionDao
however doesn't seem to be managed at all.Given the setup so far, I'd start marking your
DefaultQuestionDao
as a@Stateless
EJB. The whole setup is by the way confusing at best, the stacktrace tells aboutQuestionManagerEJB
which I don't see back in the code snippets, only aDefaultQuestionDao
and aQuestionBusinessLocal
. Perhaps you need to inject theDefaultQuestionDao
as@EJB
in yourQuestionManagerEJB
as well.Further, there's another problem: that
@NamedQuery
has got to be annotated on an@Entity
class or one of its superclasses, or it won't be found.