gpt4 book ai didi

java - Hibernate+PostgreSQL 抛出JDBCConnectionException : Cannot open connection

转载 作者:行者123 更新时间:2023-11-29 11:54:40 25 4
gpt4 key购买 nike

我编写了一个测试 Java 应用程序并且它工作正常但是这个 Web 应用程序抛出一个异常,就像对同一个 cfg.xml 文件一样

<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.release_mode">auto</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.default_schema">public</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<property name="hibernate.ejb.discard_pc_on_close">false</property>
<property name="hibernate.query.jpaql_strict_compliance">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.transaction.flush_before_completion">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
<property name="hibernate.connection.pool_size">100</property>

当我按下“保存”按钮时,出现以下异常:

javax.servlet.ServletException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)

root cause

javax.faces.el.EvaluationException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

root cause

org.hibernate.exception.JDBCConnectionException: Cannot open connection
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:98)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
$Proxy108.beginTransaction(Unknown Source)
com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:196)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

root cause

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
$Proxy108.beginTransaction(Unknown Source)
com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:196)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

block 引用

最佳答案

遇到异常时,值得在堆栈跟踪中检查最底层的根本原因。在这种情况下,它是以下内容:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres

这仅意味着 URL 错误或当前运行时类路径中不存在所需的驱动程序。由于 URL 根据 PostgreSQL JDBC documentation 看起来不错,这将是当前运行时类路径中缺少驱动程序。

因此,要解决此问题,您需要将 JDBC 驱动程序 JAR 文件放在 Web 应用程序的运行时类路径中。 /WEB-INF/lib 是 webapp 的默认运行时类路径所涵盖的文件夹之一。只需将 PostgreSQL JDBC 驱动程序 JAR 文件放在那里并重新部署。

关于java - Hibernate+PostgreSQL 抛出JDBCConnectionException : Cannot open connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071054/

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