failed.org.hibernate.exception.JDBCConnectionException:使用PostGres Hibernate调用Driver#connect时出错

时间:2014-05-15 17:02:19

标签: java hibernate postgresql maven

我一直收到此错误,这是我的文件

hibernate.cfg.xml中

    

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">postgres></property>

    <property name="hibernate.default_schema">hibernate_test</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQL82Dialect</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">update</property>

    <mapping class="com.de.entity.Employee"></mapping>

</session-factory>

HibernateUtil.java

package com.de.config;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Sets up the connection the DB and Hibernate
 * @author de
 *
 */
public class HibernateUtil {

    private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);

    private static final SessionFactory sessionFactory = buildSessionFactory(); 


    private static SessionFactory buildSessionFactory() {
        logger.info("Creating Hibernate Session Factory");
        try{
            // Create the SessionFactory form hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            logger.info("Hibernate Configuration loaded");
            logger.info("Configuration: " + configuration.getProperties());
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
        } catch (Throwable ex) {
            logger.error("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

这是一个更好的错误摘录

09:47:23,829  WARN DriverManagerConnectionProviderImpl:93 - HHH000402: Using Hibernate built-in connection pool (not for production use!)
09:47:23,846  INFO DriverManagerConnectionProviderImpl:166 - HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/postgres]
09:47:23,848  INFO DriverManagerConnectionProviderImpl:175 - HHH000046: Connection properties: {user=postgres, password=****}
09:47:23,849  INFO DriverManagerConnectionProviderImpl:180 - HHH000006: Autocommit mode: false
09:47:23,852  INFO DriverManagerConnectionProviderImpl:102 - HHH000115: Hibernate connection pool size: 1 (min=1)
09:47:23,922 ERROR HibernateUtil:35 - Initial SessionFactory creation failed.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect

我一直在努力弄清楚这个问题是什么。 我下载了Navicat for Postgres,我能够使用连接URL和用户名/密码成功连接到它。所以我缩小了连接信息是不正确的。

刚刚完成......

这是我的 的的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.de.test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>testMaven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <slf4jVersion>1.7.7</slf4jVersion>
    </properties>

    <dependencies>

        <!-- Persistence -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.2.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
    <!--    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.2.GA</version>
        </dependency> -->

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1100-jdbc41</version>
        </dependency>

        <dependency>
            <groupId>com.github.dzwicker.dart</groupId>
            <artifactId>dart-maven-plugin</artifactId>
            <version>3.0.8</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>



        <!-- REST / WEB -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.8</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.8</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.8</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.8</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.2.3</version>
        </dependency>


        <!-- Logging -->

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4jVersion}</version>
        </dependency>


        <!-- Testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <finalName>test</finalName>
    </build>
</project>

2 个答案:

答案 0 :(得分:2)

我觉得自己像个白痴。 密码中有拼写错误。

<property name="connection.password">postgres></property>

直到我介绍了jdbc类并发现了psql错误,我才意识到这一点。

答案 1 :(得分:0)

验证数据库是否已启用。那发生在我的案子中