错误:
log4j:WARN找不到记录器的appender (org.hibernate.cfg.annotations.Version)。 log4j:WARN请初始化 log4j系统正常。线程" main"中的例外情况 org.hibernate.exception.GenericJDBCException:无法打开连接 在 org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) 在 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) 在 org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420) 在 org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144) 在org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119) 在 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57) 在 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326) 在org.koushik.hibernate.HibernateTest.main(HibernateTest.java:20) 引起:org.postgresql.util.PSQLException:致命:数据库 " 5432 / hibernatedb"不存在于 org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:691) 在 org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207) 在 org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65) 在org.postgresql.jdbc.PgConnection。(PgConnection.java:159)at org.postgresql.Driver.makeConnection(Driver.java:415)at org.postgresql.Driver.connect(Driver.java:283)at java.sql.DriverManager.getConnection(未知来源)at java.sql.DriverManager.getConnection(未知来源)at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110) 在 org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417) ......还有5个
码
hibernate.cfg.xml中
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost/5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="org.javabrains.koushik.dto.UserDetails" />
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
UserDetails.java
package org.javabrains.koushik.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userID;
private String userName;
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
HibernateTest.java
package org.koushik.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.javabrains.koushik.dto.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserID(1);
user.setUserName("First user");
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session =sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
答案 0 :(得分:2)
您的连接网址似乎可能是错误。不应该是
jdbc:postgresql://localhost:5432/hibernatedb
答案 1 :(得分:0)
检查您的连接网址路径,它应该是这样的:
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
如果这不起作用,请注意在PostgreSQL的情况下,如果可用,则使用端口5432是典型的。如果不是,大多数安装人员会选择下一个空闲端口,通常是5433.所以你可以输入这样的属性
<property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb</property>
如果你仍然无法弄清楚然后在你的堆栈跟踪中读取这一行,你可能会想到@RemyG试图说的话:
org.postgresql.util.PSQLException: FATAL: database "5432/hibernatedb" does not exist at