eclipse java.sql.SQLException: 找不到适合 jdbc:mysql://localhost:3306/sushi 的驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11758385/
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
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sushi
提问by Picarus
I have a web application using a jar file(lib) to access a Database. The jar file when used as a standalone application executes correctly but the webapp received the error:
我有一个使用 jar 文件(lib)访问数据库的 Web 应用程序。用作独立应用程序时的 jar 文件可以正确执行,但 webapp 收到错误:
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
I have read the answers to similar questions but none of them solve my problem.
我已经阅读了类似问题的答案,但没有一个能解决我的问题。
I am using Maven, where I have added the dependency for the mysql version I am using, 5.1.21. In fact, I have added it for both the Lib and the Webapp.
我正在使用 Maven,我在其中添加了我正在使用的 mysql 版本 5.1.21 的依赖项。事实上,我已经为 Lib 和 Webapp 添加了它。
Before that, I tried to define in Eclipse a Connectivity Driver Definition bound to the same file, that I had copied to WEB-INF/lib in the eclipse project for my Webapp and with the same parameters that I include below for the persistence.xml file
在此之前,我尝试在 Eclipse 中定义一个绑定到同一文件的连接驱动程序定义,我已将其复制到我的 Webapp 的 eclipse 项目中的 WEB-INF/lib 并使用我在下面为persistence.xml 包含的相同参数文件
I am not using any java to configure since I do all the configuration in the persistence.xml file(that I have copied to both META-INF folders, the one for the app(lib) and the one for the webapp. I am using Hibernate (4.1.2) through JPA.
我没有使用任何 java 进行配置,因为我在 persistence.xml 文件中进行了所有配置(我已将其复制到 META-INF 文件夹中,一个用于应用程序(lib),另一个用于 webapp。我正在使用通过 JPA 进行休眠 (4.1.2)。
The persistence.xml file is like that:
persistence.xml 文件是这样的:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myappPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
the exception that I get is at Runtime, when launching an Http Request to the app, not at the initialization of the webapp, where I haven't seen any errors but this warning appears:
我得到的例外是在运行时,当向应用程序启动 Http 请求时,而不是在 web 应用程序的初始化时,我没有看到任何错误,但出现此警告:
[01-08-12 13:16:39] - 8782 WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://localhost:3306/myapp
[01-08-12 13:16:39] - 8782 WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342:无法获得查询元数据的连接:找不到适合 jdbc 的驱动程序:mysql://localhost: 3306/我的应用程序
I guess this is due to the information being looked up from the database is not necessary at init, and no exception is thrown but the same root cause in both cases.
我想这是因为在 init 时不需要从数据库中查找的信息,并且没有抛出异常,但在这两种情况下根本原因相同。
EDIT:I am using Tomcat 6.0
编辑:我正在使用 Tomcat 6.0
Any help will be highly appreciated.
任何帮助将不胜感激。
回答by Rabin Pantha
It seems that you are missing MySql driver in your project. To solve this you need to add mysql-connector.jar. externally to your project. Or if you are using a maven project then use the following dependency in your pom.xml file.
您的项目中似乎缺少 MySql 驱动程序。要解决这个问题,您需要添加 mysql-connector.jar。在您的项目外部。或者,如果您使用的是 Maven 项目,则在 pom.xml 文件中使用以下依赖项。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
You can use a suitable version if needed!!
如果需要,您可以使用合适的版本!!
回答by Buhake Sindi
If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.
如果您将应用程序作为 Java 应用程序运行,请在 Eclipse 的 Java 构建路径中添加 JAR 文件。
Alternatively, I wouldn't put the MSQL jar inside WEB-INF/lib
folder of your web project, instead, I would put it in your Application folder library folder, where the Application Server will load your driver and you can access it from the Application Server container.
或者,我不会将 MSQL jar 放在WEB-INF/lib
您的 Web 项目的文件夹中,而是将它放在您的应用程序文件夹库文件夹中,应用程序服务器将在其中加载您的驱动程序,您可以从应用程序服务器容器访问它。
回答by Saunik Singh
you should put mysql connector jar with your own code & add jar file with your project.
您应该将 mysql 连接器 jar 与您自己的代码放在一起,并在您的项目中添加 jar 文件。
回答by Ankit Pawar
I also had the same problem some time before, but I solved that issue. There may be different reasons for this exception.
我之前也遇到过同样的问题,但我解决了这个问题。此异常可能有不同的原因。
One of them may be that the jar
you are adding to your lib
folder may be old. Try to find out the latest mysql-connector-jar versionand add that to your classpath
. It may solve your issue.
其中之一可能是jar
您添加到lib
文件夹中的文件可能已经过时。尝试找出最新的 mysql-connector-jar 版本并将其添加到您的classpath
. 它可能会解决您的问题。