Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 300
user2875914 Avatar asked Jan 05 '15 18:01

user2875914


2 Answers

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

mysql-connector-java 6.0.4

like image 171
Stan Sokolov Avatar answered Oct 13 '22 16:10

Stan Sokolov


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>
like image 2
UsamaAmjad Avatar answered Oct 13 '22 18:10

UsamaAmjad