我在制作网络应用程序时遇到了麻烦。 我使用的是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;
}
}
}
非常感谢!
答案 0 :(得分:0)
将此添加到servlet初始化:
2009
JDBC驱动程序在其类静态初始化程序中自行注册,调用上面的代码会导致加载类并执行静态初始化。您只需在应用程序启动时调用一次。
另请参阅Postgre JDBC文档:https://jdbc.postgresql.org/documentation/head/load.html