簡體   English   中英

SQLException:沒有為jdbc:postgresql找到合適的驅動程序

[英]SQLException: No suitable driver found for jdbc:postgresql

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)

正如Nicolas所說,將JDBC驅動程序安裝為核心模塊

接下來你需要讓你的應用程序依賴於這個模塊有兩種方式:MANIEST.MF jboss-dependency.xml

我推薦第一種方式,因為它更符合規范。 基本上,您需要在EAR或WAR的META-INF目錄中創建MANIFEST.MF文件(無論正在部署什么)。 您在文件中創建的行將類似於:

Dependencies: org.postgresql

有關詳細信息,請參閱JBoss EAP文檔JBoss wiki 我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM