Hibernate + PostgreSQL引发异常:找不到JDBC驱动程序类:org.postgresql.Drive

时间:2015-07-03 17:58:27

标签: java hibernate web-services postgresql jdbc

我使用jersey + hibernate编写了一个用于restful webservice的Java maven项目并且犯了这个错误:

javax.servlet.ServletException: org.hibernate.HibernateException: JDBC 
Driver class not found: org.postgresql.Drive
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
221)

根本原因:

org.hibernate.HibernateException: JDBC Driver class not found: 
org.postgresql.Drive
org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverMan
agerConnectionProvider.java:89)
org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(Con
nectionProviderFactory.java:143)
org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(Con
nectionProviderFactory.java:84)
org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.j
ava:459)
org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:90)
org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:286
3)
org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
org.asad.login.login.loginservice.LoginService.getDatabaseUser(LoginService.
java:25)
org.asad.login.login.resources.LoginResource.get(LoginResource.java:21)
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.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFa
ctory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatc
her$1.run(AbstractJavaResourceMethodDispatcher.java:143)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatc
her.invoke(AbstractJavaResourceMethodDispatcher.java:160)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvi
der$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatc
her.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMetho
dInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethod    Invoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethod    Invoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.j    ava:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.jav
a:1072)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
221)

poml.xml文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    enter code here
    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>org.asad.login</groupId>

    <artifactId>login</artifactId>

    <packaging>war</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <name>login</name>

    <build>

        <finalName>login</finalName>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>


                <version>2.5.1</version>

                <inherited>true</inherited>

                <configuration>

                    <source>1.7</source>

                    <target>1.7</target>

                </configuration>

            </plugin>


        </plugins>

    </build>

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.glassfish.jersey</groupId>

                <artifactId>jersey-bom</artifactId>

                <version>${jersey.version}</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

    <dependencies>

        <dependency>

            <groupId>org.glassfish.jersey.containers</groupId>

            <artifactId>jersey-container-servlet-core</artifactId>

            <!-- use the following artifactId if you don't need servlet 2.x 

compatibility -->

            <!-- artifactId>jersey-container-servlet</artifactId -->

        </dependency>


     <dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>javax.servlet-api</artifactId>

  <version>3.1.0</version>

  <scope>provided</scope>

        </dependency>

    <dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-envers</artifactId>

    <version>3.6.4.Final</version>

    </dependency>


    <dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-validator</artifactId>

    <version>4.2.0.Final</version>

</dependency>



    </dependencies>



   <properties>

        <jersey.version>2.16</jersey.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

</project>

JDBC驱动程序位于类路径中。

hibernate.cfg.xml文件:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->

        <property name="connection.driver_class">org.postgresql.Drive</property>

        <property 

name="connection.url">jdbc:postgresql://localhost:5432/logindb</property>

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

        <property name="connection.password">project</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>



        <!-- Disable the second-level cache  -->

        <property 

name="cache.provider_class">org.hibernate.cache.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>


        <!-- Names the annotated entity class -->

        <mapping class="org.asad.dto.DatabaseClass"/>

    </session-factory>

</hibernate-configuration>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

将PostgreSQL maven依赖项添加到您的pom文件

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901-1.jdbc4</version>
</dependency>

并在驱动程序

中添加缺少的r
<property name="connection.driver_class">org.postgresql.Driver</property>

答案 1 :(得分:0)

此行中的错误:

<property name="connection.driver_class">org.postgresql.Drive</property>

上面写着Drive,应该将其更改为Driver

<property name="connection.driver_class">org.postgresql.Driver</property>