2011-06-02 61 views
1

已解決:只需在JAVA_HOME \ jre \ lib \ ext中放入mysql-connector-java-5.1.x.jar即可。JPA,Maven和MySQL。配置錯誤。未找到類[com.mysql.jdbc.Driver]

您好所有,我剛開始構建使用Maven的,MySQL和JPA演示應用程序,這是我的代碼: 的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

<modelVersion>4.0.0</modelVersion> 
<groupId>org.ee6.book</groupId> 
<artifactId>chapter02</artifactId> 
<version>2.0</version> 
<name>Chapter 02 - JPA</name> 


<dependencies> 
    <dependency> 
     <groupId>org.eclipse.persistence</groupId> 
     <artifactId>javax.persistence</artifactId> 
     <version>2.0.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.eclipse.persistence</groupId> 
     <artifactId>eclipselink</artifactId> 
     <version>2.0.1</version> 
    </dependency> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.6</version> 
    </dependency> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.6</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.1</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

和我的persistence.xml:

<persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL"> 
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<class>org.ee6.book.chapter02.Book</class> 
<properties> 
    <property name="eclipselink.target-database" value="MYSQL"/> 
    <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 
    <property name="eclipselink.logging.level" value="SEVERE"/> 
    <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
    <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/chapter02DB"/> 
    <property name="eclipselink.jdbc.user" value="root"/> 
    <property name="eclipselink.jdbc.password" value="xyz"/>  
</properties> 

當執行MVN編譯它就好了,但是當我嘗試執行主類

mvn exec:java -Dexec.mainClass="org.ee6.book.chapter02.Main" 

我有錯誤,如:

[EL Severe]: 2011-06-03 00:23:27.89--ServerSession(26373776)--Local Exception Stack: 
Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException 
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found. 
     at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:82) 
     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.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:327) 
     at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:295) 
     at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:415) 
     at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:155) 
     at org.eclipse.persistence.sessions.server.ConnectionPool.startUp(ConnectionPool.java:433) 
     at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:495) 
     at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:632) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:230) 
     at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:369) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195) 
     at org.ee6.book.chapter02.Main.main(Main.java:20) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:291) 
     at java.lang.Thread.run(Thread.java:662) 

我使用maven 2.2。 1。任何幫助將不勝感激!謝謝

回答

1

這可能是因爲你在你的pom中有兩次mysql-dependency(mysql-connector-java)。一次使用範圍測試,一次使用默認範圍。用測試範圍刪除一個將有望解決你的分類問題。

+0

遺憾。問題依然存在。 – 2011-06-04 03:02:07

3

只需添加一個依賴關係到MySQL的Java連接器與運行範圍:

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