SQLException:没有为jdbc找到合适的驱动程序:postgresql

时间:2016-09-01 17:27:23

标签: java postgresql java-ee jdbc jboss

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

entityManager.getTransaction()

引发

JBoss 7.1.1

Class.forName("org.postgresql.Driver");
EntityManager entityManager = Persistence.createEntityManagerFactory("database2").createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="database2" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/database2" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

目录结构

EJB:
    |
    |- src
            |
            |- class1.java
            |- interface1.java
            |
            |- META_INF
                        |
                        |-persistence.xml

WEB:
    |
    |- src
    |       |
    |       |- class2.java
    |
    |-WebContent
                |
                |- WEB-INF
                            |
                            |- lib
                                    |
                                    |- postgresql-9.1-903.jdbc4.jar

EAP_HOME /模块/组织/ PostgreSQL的/主/ module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.postgresql">
  <resources>
    <resource-root path="postgresql-9.1-903.jdbc4.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
  </dependencies>
</module>

CLI命令:

/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=org.postgresql,driver-xa-datasource-class-name=org.postgresql.Driver)

1 个答案:

答案 0 :(得分:0)

正如Nicolas所说,将JDBC驱动程序安装为core module

接下来你需要让你的应用程序依赖于这个模块有两种方法:    MANIEST.MF    JBoss的-dependency.xml

我推荐第一种方式,因为它更符合规范。基本上,您需要在EAR或WAR的META-INF目录中创建MANIFEST.MF文件(无论正在部署什么)。您在文件中创建的行将类似于:

Dependencies: org.postgresql

有关详细信息,请参阅JBoss EAP documentationJBoss wiki。我希望这有帮助!