当我尝试在 Tomcat 7 上运行我的 JSF 应用程序时,它会引发此异常。
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 51 more
如果我在创建 EntityManagerFactory 之前添加此行,它工作正常。
Class.forName("com.mysql.jdbc.Driver");
emf = Persistence.createEntityManagerFactory("manager1");
我的依赖是
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.2.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.19</version>
</dependency>
我的应用程序也可以正常运行 tomcat 6,无需添加Class.forName("com.mysql.jdbc.Driver");
有什么想法可以解决这个问题吗?谢谢