java 找不到适合 jdbc 的驱动程序:mysql://localhost:3306/test

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10354997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:41:00  来源:igfitidea点击:

No suitable driver found for jdbc:mysql://localhost:3306/test

javahibernatetomcatjpa

提问by mkayman

When i try to run my JSF application on Tomcat 7, it throws this exception.

当我尝试在 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

If I add this line before creating EntitiyManagerFactory, it works fine.

如果我在创建 EntitiyManagerFactory 之前添加这一行,它工作正常。

Class.forName("com.mysql.jdbc.Driver");
emf = Persistence.createEntityManagerFactory("manager1");

My dependencies are

我的依赖是

<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>

Also my application works fine tomcat 6, without adding Class.forName("com.mysql.jdbc.Driver");

我的应用程序也可以正常运行 tomcat 6,无需添加 Class.forName("com.mysql.jdbc.Driver");

Any idea to solve this problem? Thanks

有什么想法可以解决这个问题吗?谢谢

回答by JBirch

First and real solution: Try with Hibernate 4.1.0 and 4.1.3.Final.

第一个也是真正的解决方案:尝试使用 Hibernate 4.1.0 和 4.1.3.Final。

I ran into this issue on Hibernate 4.1.2 before eventually stumbling across https://forum.hibernate.org/viewtopic.php?p=2454336. As far as I can see, it's a bit of an incompatibility between the later versions of Tomcat (In its fixes for the leaky nature of the DeviceManager) and Hibernate 4.1.2.

在最终偶然发现https://forum.hibernate.org/viewtopic.php?p=2454336之前,我在 Hibernate 4.1.2 上遇到了这个问题。据我所知,Tomcat 的更高版本(修复了 DeviceManager 的泄漏性质)和 Hibernate 4.1.2 之间有点不兼容。

The other workarounds are to invoke the registration of the driver explicitly, as in Brad Whitaker's answer, or to make sure the JreMemoryLeakPreventionListener isn't protecting the DriverManager by adding driverManagerProtection="false"to the Listener definition in $CATALINA_HOME/conf/server.xml - that is:

其他解决方法是显式调用驱动程序的注册,如 Brad Whitaker 的回答,或者通过添加driverManagerProtection="false"到 $CATALINA_HOME/conf/server.xml 中的侦听器定义来确保 JreMemoryLeakPreventionListener 没有保护 DriverManager - 即:

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false">

回答by Brad Whitaker

I encountered this same problem when I migrated a working .war from Tomcat 5.5 and Java 6 to Tomcat 7 and Java 7. My previously working .war started throwing "java.sql.SQLException: No suitable driver found for jdbc:sqlserver...". I was able to resolve by simply adding

当我将工作 .war 从 Tomcat 5.5 和 Java 6 迁移到 Tomcat 7 和 Java 7 时,我遇到了同样的问题。我以前工作的 .war 开始抛出“java.sql.SQLException:找不到适合 jdbc:sqlserver 的驱动程序... ”。我可以通过简单地添加来解决

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

immediately before invoking

在调用之前立即

java.sql.DriverManager.getConnection("myUrl");

I'm aware that documentation says this additional call is not required with the jdbc4 driver, but this fixed my problem. The driver jar (sqljdbc4.jar) was located in WEB-INF/lib of my .war.

我知道文档说 jdbc4 驱动程序不需要这个额外的调用,但这解决了我的问题。驱动程序 jar (sqljdbc4.jar) 位于我的 .war 的 WEB-INF/lib 中。

回答by Jigar Joshi

The problem is driver class isn't there available at runtime for your application, put the driver jar into /WEB-INF/lib

问题是驱动程序类在您的应用程序运行时不可用,请将驱动程序 jar 放入 /WEB-INF/lib

回答by djmj

On some servers (jboss, glassfish) I also had to put the driver jar into the server default library folder.

在某些服务器(jboss、glassfish)上,我还必须将驱动程序 jar 放入服务器默认库文件夹中。

回答by duckegg

I have similar issue. It is solved by upgrading hibernate to 4.1.4final.

我有类似的问题。通过将 hibernate 升级到 4.1.4final 来解决。