【问题标题】:JBDS HHH000319: Could not get database metadata: java.sql.SQLException: No suitable driver found [duplicate]JBDS HHH000319:无法获取数据库元数据:java.sql.SQLException:找不到合适的驱动程序 [重复]
【发布时间】:2016-01-01 03:39:01
【问题描述】:

我无法让 web 应用正常工作。 我正在使用 Red Hat JBoss Developer Studio 8.1.0.GA,我在部署时遇到了错误。

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

这是我的 persistence.xml

<persistence-unit name="HibernateProject"
    transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>com.test.model.UserDetails</class>
    <properties>
        <property name="hibernate.connection.driver.class" value="org.postgresql.Driver"></property>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.connection.url"
            value="jdbc:postgresql://localhost:5432/hibernatedb"></property>
        <property name="hibernate.connection.username" value="postgres"></property>
        <property name="hibernate.connection.password" value="1234"></property>
    </properties>
</persistence-unit>

这是我的 servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    UserDetails user = new UserDetails();
    user.setUserName(request.getParameter("username"));

    //use persistence-unit name from persistence.xml
    EntityManagerFactory entityFactory = Persistence.createEntityManagerFactory("HibernateProject");

    EntityManager entityManager = entityFactory.createEntityManager();

    EntityTransaction entityTransaction = null;

    try{
        entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
        entityManager.persist(user);
        entityTransaction.commit();
    }catch(RuntimeException re){
        if(entityTransaction.isActive()){
            entityTransaction.rollback();
            throw re;
        }
    }

}

非常感谢!

【问题讨论】:

    标签: java hibernate jpa jboss


    【解决方案1】:

    将此添加到您的 servlet 初始化中:

    Class.forName("org.postgresql.Driver");
    

    JDBC 驱动程序在它们的类静态初始化器中自行注册,调用上述会导致类被加载并执行静态初始化。您只需在应用程序启动时调用一次。

    另见 Postgre JDBC 文档:https://jdbc.postgresql.org/documentation/head/load.html

    【讨论】:

    • 还是不行:(
    • 我发现了问题!!。原来我在persistence.xml中写了hibernate.connection.driver.class而不是hibernate.connection.driver_class
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2013-07-19
    • 2023-03-18
    • 1970-01-01
    • 2014-08-06
    • 2013-07-04
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多