ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
Solution 1
Try com.mysql.cj.jdbc.MysqlDataSource
. It works with
mysql-connector-java 6.0.4
Solution 2
So I was facing the same problem after upgrading Spring
version. 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 Driver
used to be at com.mysql.jdbc.Driver
but now it is located at com.mysql.cj.jdbc.Driver
. So you need to update your data source definition.
<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:
<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>
Solution 3
HikariCP states not using driver configuration, you should defined jdbcUrl for MySQL:
MySQL Connector/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.
user2875914
Updated on June 24, 2022Comments
-
user2875914 almost 2 years
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.
This is my 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.
-
roeygol over 9 yearsTry to look the needed file in the downloaded jar you got
-
Nemolovich over 9 yearsWhat do you use as application server? Maybe you'll need to set an environment variable to define the path of your library.
-