簡體   English   中英

配置錯誤。 找不到 Class [com.mysql.cj.jdbc.Driver]。 EclipseLink, MySQL 驅動程序

[英]Configuration error. Class [com.mysql.cj.jdbc.Driver] not found. EclipseLink, MySQL driver

我正在嘗試使用 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 連接器下載了所有必需的 jars,我仍收到異常描述:配置錯誤。 找不到 Class [com.mysql.cj.jdbc.Driver]。 以下是我使用的版本:1. Mysql JDBC 連接器:8.0.11 2. Eclipselink:2.7.1 3. JPA:2.2.0

附上我的Maven依賴jars的截圖。

我嘗試了 stackoverflow 上其他線程的所有建議,但無法解決問題。

感謝您的幫助,並提前致謝!

下面是我的堅持。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]: server: 2018-06-24 17:33:10.619--Thread(Thread[main,5,main])--配置的服務器平台:org.eclipse.persistence.platform.server.NoServerPlatform [EL Info ]: 2018-06-24 17:33:11.065--ServerSession(1101598632)--Thread(Thread[main,5,main])--EclipseLink,版本:Eclipse 持久性服務 - 2.6.5.v20170607-b3d05bd [EL嚴重]: ejb: 2018-06-24 17:33:11.068--ServerSession(1101598632)--Thread(Thread[main,5,main])--Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.6.5 .v20170607-b3d05bd): org.eclipse.persistence.exceptions.DatabaseException 異常描述:配置錯誤。 找不到 Class [com.mysql.cj.jdbc.Driver]。 線程“main”中的異常 javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.6.5.v20170607-b3d05bd): org.eclipse.persistence.exceptions.DatabaseException 異常描述:配置錯誤。 找不到 Class [com.mysql.cj.jdbc.Driver]。 at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:818) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:207) at org.eclipse.persistence.internal .jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:307) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl .java:303) at practice.JPA.persistence.App.main(App.java:17) Caused by: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.6.5.v20170607-b3d05bd): org8878317887831 .exceptions.DatabaseException異常描述:Con 形象錯誤。 找不到 Class [com.mysql.cj.jdbc.Driver]。 at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:89) at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267) at org.eclipse.persistence.sessions.DefaultConnector.connect (DefaultConnector.java:85) at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162) at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:214) at org. eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:776) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265) at org.eclipse.persistence.internal.jpa. EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.882132469458 88:734)... 還有 5 個

我的項目使用 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>

我想我來不及了,但可以幫助任何人。 在我的例子中,我使用的是 mysql-connector-java3.1.11 並得到相同的錯誤。 在嘗試了一些其他無效的解決方案后,我檢查了“persistence.xml”的源代碼並更改了以下行: <property name="javax.persistence.cj.jdbc.driver" value="com.mysql.jdbc.Driver"/><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> (從字符串中刪除“cj”)之后,我的程序運行沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM