SQLException:没有为jdbc:postgresql找到合适的驱动程序 [英] SQLException: No suitable driver found for jdbc:postgresql

查看:262
本文介绍了SQLException:没有为jdbc:postgresql找到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.sql.SQLException:找不到合适的jdbc驱动程序:postgresql:// localhost:5432 / database2

引发 entityManager.getTransaction()

JBoss 7.1.1

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 / modules /org/postgresql/main/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命令:

/子系统=数据源/ JDBC驱动器的PostgreSQL =:添加(驱动程序名称= PostgreSQL中,驱动器模块名= org.postgresql,驾驶员XA-数据源类名= org.postgresql.Driver)

推荐答案

正如Nicolas所说,将JDBC驱动程序安装为核心模块

As Nicolas says install the JDBC driver as a core module

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

Next you need to make you application depend on this module there are two ways: MANIEST.MF jboss-dependency.xml

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

I recommend the first way as it is more spec compliant. Essentially you need to create a MANIFEST.MF file in the META-INF directory of your EAR or WAR (whatever is being deployed). The line you create in the file will be something like:

Dependencies: org.postgresql

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

For further info see the JBoss EAP documentation or the JBoss wiki. I hope this helps!

这篇关于SQLException:没有为jdbc:postgresql找到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆