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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27785324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:23:53  来源:igfitidea点击:

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

javamysqlmavenjdbc

提问by user2875914

I am using the Hikari library for MySQL connections in my project. When I attempt to run the program I get a ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource error. I figured I would have to include the mysql-connector library in my project.

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

This is my pom.xml

这是我的 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>

The jar is built successfully however the error still occurs. Any help is very appreciated. Thanks.

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

回答by Stan Sokolov

Try com.mysql.cj.jdbc.MysqlDataSource. It works with

试试com.mysql.cj.jdbc.MysqlDataSource。它与

mysql-connector-java 6.0.4

mysql-connector-java 6.0.4

回答by user7294900

HikariCPstates not using driver configuration, you should defined jdbcUrl for MySQL:

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

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

MySQL 连接器/J com.mysql.jdbc.jdbc2.optional.MysqlDataSource

The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.

已知 MySQL 数据源在网络超时支持方面被破坏。请改用 jdbcUrl 配置。

回答by user2875914

I managed to fix this by setting "minimizeJar" to false. I'm not too familiar with maven so I don't know how this fixed it but it did.

我设法通过将“minimizeJar”设置为 false 来解决此问题。我对 maven 不太熟悉,所以我不知道这是如何解决的,但确实如此。

回答by UsamaAmjad

So I was facing the same problem after upgrading Springversion. The question already has the correct answer but lacks the details. To connect to the data source (ex: MySQL) you need to have proper drivers. Previously the Driverused to be at com.mysql.jdbc.Driverbut now it is located at com.mysql.cj.jdbc.Driver. So you need to update your data source definition.

所以升级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>

Hikari Example:

Hikari 示例:

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