0

I'm having trouble making a webapp work. i am using Red Hat JBoss Developer Studio 8.1.0.GA and i encountered an error when deploying.

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

here is my 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>

here is my 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;
        }
    }

}

Thanks alot!

pat3ck029
  • 263
  • 1
  • 7
  • 19

1 Answers1

0

Add this to your servlet initialization:

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

The JDBC drivers self-register in their class static initializer, calling the above causes the class to be loaded and the static initialization to be performed. You only need to call this once on application start-up.

See also Postgre JDBC documentation: https://jdbc.postgresql.org/documentation/head/load.html

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43