Java 冬眠+;PostgreSQL抛出JDBCConnectionException:无法打开连接
我编写了一个测试Java应用程序,它工作正常,但是这个Web应用程序对同一个cfg.xml文件抛出了类似的异常Java 冬眠+;PostgreSQL抛出JDBCConnectionException:无法打开连接,java,hibernate,postgresql,jsf,jdbc,Java,Hibernate,Postgresql,Jsf,Jdbc,我编写了一个测试Java应用程序,它工作正常,但是这个Web应用程序对同一个cfg.xml文件抛出了类似的异常 <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.autocommit">true</property> <property name="hibernat
<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>
Blockquote这里有什么问题 例外情况非常明确: org.hibernate.exception.jdbconnectionexception:无法打开连接
确保您可以连接到所连接的数据库、配置正确、没有网络问题阻止连接等。我没有查看您的配置文件,但我假设它的配置至少相当好,因为堆栈跟踪表明它正试图通过本地主机JDBC url连接到postgres数据库 看起来最大的问题是找不到postgres JDBC驱动程序: 根本原因 java.sql.SQLException:未找到适合jdbc的驱动程序:postgresql://localhost/postgres java.sql.DriverManager.getConnection(未知源)java.sql.DriverManager.getConnection(未知源)org.hibernate.connection.DriverManager ConnectionProvider.getConnection(DriverManager ConnectionProvider.java:133) 确保JAR文件位于类路径中。如果您还没有,可以在此处下载:
遇到异常时,值得检查stacktrace中最底层的根本原因。在这种情况下,它如下所示:
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres
这只意味着URL错误或者当前运行时类路径中不存在所需的驱动程序。由于URL看起来很好,因此当前运行时类路径中缺少驱动程序
因此,要解决这个问题,需要将JDBC驱动程序JAR文件放在webapp的运行时类路径中。
/WEB-INF/lib
是webapp默认运行时类路径所涵盖的文件夹之一。只需将PostgreSQL JDBC驱动程序JAR文件放在那里,然后重新部署。验证您的数据库驱动程序的JAR是否位于类路径中。
验证驱动程序类名是否正确。
验证connection.url是否正确。
我也犯了同样的错误。唯一的区别是,我使用的是Oracle11g。我在类路径中有ojdbc14.jar,驱动程序类名是正确的,但connection.url属性是错误的。
Oracle 11g的正确格式为
jdbc:oracle:thin:@localhost:1521:global\u dsn\u name但我这样配置hibernate cfg.xml;org.postgresql.Driver 1234 jdbc:postgresql://localhost/postgres postgres public org.hibernate.dialene.PostgreSqlDialenOK,您是否有一个postgres数据库在默认端口的本地主机上运行,是否有一个名为postgres的数据库,它有一个名为postgres的用户,密码名为postgres吗?是的,有一个名为postgres的数据库,使用默认端口。这个数据库有一个名为postgres的用户,其pass='1234',这个答案有点粗鲁。没有。我写了一个测试Java应用程序,它工作正常,但我认为这个Web应用程序有一个问题。我控制JDBC驱动程序,它是正确的驱动程序路径已经是/WEB-INF/lib,但同样的问题仍然存在,显然使用了不同的类加载器。尝试在
Tomcat/conf/catalina.properties
中放入Tomcat/lib
或将JAR文件的根路径添加到shared.loader
。
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres