ClassNotFoundException:com.mysql.jdbc.jdbc2.optional.MysqlDataSource

时间:2015-01-05 18:09:56

标签: java mysql maven jdbc

我在项目中使用Hikari库进行MySQL连接。当我尝试运行该程序时,我得到一个ClassNotFoundException:com.mysql.jdbc.jdbc2.optional.MysqlDataSource错误。我想我必须在我的项目中包含mysql-connector库。

这是我的pom.xml

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.0.Beta3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>GridControl</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Main-Class>net.thegridmc.control.GridControl</Main-Class>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <minimizeJar>true</minimizeJar>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <artifactSet>
                                <includes>
                                    <include>mysql:*</include>
                                    <include>org.slf4j:*</include>
                                    <include>com.zaxxer:*</include>
                                    <include>org.javassist:javassist</include>
                                    <include>io.netty:netty-all</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

jar已成功构建,但错误仍然存​​在。非常感谢任何帮助。感谢。

4 个答案:

答案 0 :(得分:10)

试试com.mysql.cj.jdbc.MysqlDataSource。它适用于

mysql-connector-java 6.0.4

答案 1 :(得分:0)

我设法通过设置&#34; minimizeJar&#34;来解决这个问题。为假。我对maven不太熟悉,所以我不知道如何修复它,但确实如此。

答案 2 :(得分:0)

因此,在升级Spring版本之后,我面临着同样的问题。该问题已经有正确答案,但缺少细节。要连接到数据源(例如:MySQL),您需要具有适当的驱动程序。以前的Driver曾经位于com.mysql.jdbc.Driver,但是现在它位于com.mysql.cj.jdbc.Driver。因此,您需要更新数据源定义。

<bean id="dataSource" class="com.mysql.cj.jdbc.MysqlDataSource"
        destroy-method="close">
    <property name="url" value="${jdbc.url}" /> <!-- Loaded from application.properties file -->
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxIdle" value="1" />
</bean>

光的例子:

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP" />
    <property name="connectionTestQuery" value="SET NAMES utf8mb4" />
    <property name="dataSourceClassName" value="com.mysql.cj.jdbc.MysqlDataSource" />
    <property name="dataSourceProperties">
        <props>
            <prop key="url">${jdbc.url}</prop> <!-- Loaded from application.properties file -->
            <prop key="user">${jdbc.username}</prop>
            <prop key="password">${jdbc.password}</prop>
        </props>
    </property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
    destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean>

答案 3 :(得分:0)

HikariCP声明未使用驱动程序配置,您应该为MySQL定义jdbcUrl:

MySQL Connector / J com.mysql.jdbc.jdbc2.optional.MysqlDataSource

  

众所周知,就网络超时支持而言,MySQL数据源已损坏。改为使用jdbcUrl配置。