简体   繁体   中英

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sushi

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:

[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

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.

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

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.

The persistence.xml file is like that:

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

[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

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.

EDIT:I am using Tomcat 6.0

Any help will be highly appreciated.

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.

  <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.4</version>
  </dependency>

You can use a suitable version if needed!!

If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.

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.

您应该将 mysql 连接器 jar 与您自己的代码放在一起,并在您的项目中添加 jar 文件。

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 version and add that to your classpath . It may solve your issue.

我在 Tomcat 9 上遇到了同样的问题。通过在 common/lib 和 WEB-INF/lib(使用前者)中都没有驱动程序 jar 来解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM