配置错误。找不到类[com.mysql.cj.jdbc.Driver]。 EclipseLink,MySQL驱动程序

时间:2018-06-24 20:49:19

标签: java mysql jdbc classnotfoundexception

我正在尝试使用EclipseLink学习JPA。我正在使用Mysql作为数据库。我已经添加了所有必需的依赖项,下面是我的pom.xml

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.7.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
        <scope>runtime</scope>
    </dependency>

</dependencies>

问题是,即使maven已下载了mysql jdbc连接器的所有必需jar,我仍收到异常描述:配置错误。找不到类[com.mysql.cj.jdbc.Driver]。以下是我使用的版本: 1. Mysql JDBC连接器:8.0.11 2. Eclipselink:2.7.1 3. JPA:2.2.0

随附的是我的Maven依赖项jar的屏幕截图。

我在stackoverflow上尝试了其他线程的所有建议,但无法解决问题。

感谢您的帮助,并预先感谢!

下面是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" 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">

   <persistence-unit name="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL">

      <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>

      <properties>
         <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpadb"/>
         <property name="javax.persistence.jdbc.user" value="root"/>
         <property name="javax.persistence.jdbc.password" value="root"/>
         <property name="eclipselink.logging.level" value="FINE"/>
         <property name="eclipselink.ddl-generation" value="create-tables"/>
      </properties>

   </persistence-unit>
</persistence>

此外,添加错误堆栈跟踪:

[EL Fine]:服务器:2018-06-24 17:33:10.619--Thread(Thread [main,5,main])-配置的服务器平台:org.eclipse.persistence.platform.server.NoServerPlatform [EL信息]:2018-06-24 17:33:11.065--ServerSession(1101598632)-Thread(Thread [main,5,main])-EclipseLink,版本:Eclipse Persistence Services-2.6.5.v20170607- b3d05bd [EL Severe]:ejb:2018-06-24 17:33:11.068--ServerSession(1101598632)-Thread(Thread [main,5,main])-异常[EclipseLink-4003](Eclipse Persistence Services-2.6 .5.v20170607-b3d05bd):org.eclipse.persistence.exceptions.DatabaseException 异常描述:配置错误。找不到类[com.mysql.cj.jdbc.Driver]。 线程“主”中的异常javax.persistence.PersistenceException:异常[EclipseLink-4003](Eclipse Persistence Services-2.6.5.v20170607-b3d05bd):org.eclipse.persistence.exceptions.DatabaseException 异常描述:配置错误。找不到类[com.mysql.cj.jdbc.Driver]。     在org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:818)     在org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.getAbstractSession(EntityManagerFactoryDe​​legate.java:207)     在org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.createEntityManagerImpl(EntityManagerFactoryDe​​legate.java:307)     在org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)     在org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)     在实践中.JPA.persistence.App.main(App.java:17) 由以下原因引起:异常[EclipseLink-4003](Eclipse Persistence Services-2.6.5.v20170607-b3d05bd):org.eclipse.persistence.exceptions.DatabaseException 异常描述:配置错误。找不到类[com.mysql.cj.jdbc.Driver]。     在org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:89)     在org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)     在org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)     在org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)     在org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:214)     在org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:776)     在org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)     在org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:734)     ...还有5个

1 个答案:

答案 0 :(得分:0)

我的项目使用MySQL和JPA的EclipseLink实现,并且运行良好。我的pom文件:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.7.6</version>
</dependency>