4

I made a simple Java EE app, and I have a problem with connection to database. In eclipse everything works fine, but when I try the same in Intellij errors occur.

package db;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class DbUtil {
private static DbUtil dbUtil;
private ComboPooledDataSource connectionPool;

private DbUtil() throws PropertyVetoException {
    connectionPool = new ComboPooledDataSource();
    connectionPool.setDriverClass("com.mysql.jdbc.Driver");
    connectionPool.setJdbcUrl("jdbc:mysql://localhost:3306/world");
    connectionPool.setUser("root");
    connectionPool.setPassword("root");

    connectionPool.setInitialPoolSize(5);
    connectionPool.setMinPoolSize(5);
    connectionPool.setMaxPoolSize(20);
    connectionPool.setAcquireIncrement(5);
    connectionPool.setMaxIdleTime(3600);
}

public Connection getConnection() throws SQLException {
    return connectionPool.getConnection();
}

public void close() {
    connectionPool.close();
}

public static DbUtil getInstance() {
    if (dbUtil == null) {
        try {
            dbUtil = new DbUtil();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    return dbUtil;
}
  }

In project structure - > libraries I have: image

And the errors are:

java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource
java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bartosz Czyżowski
  • 119
  • 1
  • 4
  • 13
  • if those "libraries" represent jar files in the image, at the very least you also need the jar for mchange-commons-java version 0.2.11 or above. – Steve Waldman Jun 27 '16 at 07:50

5 Answers5

4

It's been a long time, but I faced the same problem and this solution worked.

In project_name/web/WEB-INF/ create new folder named lib, copy .jars c3p0-0.9.5.2, c3p0-oracle-thin-extras-0.9.5.2, mchange-commons-java-0.2.11 then in Project View select lib folder and finally Add as liblary....

BednarQ
  • 139
  • 3
  • 15
2

Just add this dependencies to pom file

    <dependency>
        <groupId>com.google.code.maven-play-plugin.com.mchange</groupId>
        <artifactId>c3p0-oracle-thin-extras</artifactId>
        <version>0.9.5</version>
    </dependency>

    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>mchange-commons-java</artifactId>
        <version>0.2.11</version>
    </dependency>

    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
Andriy
  • 1,981
  • 1
  • 12
  • 9
1

This may be because of the servlet config

bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

Download the c3p0 jar of hibernate to the lib ad it to the jars

http://www.java2s.com/Code/Jar/c/Downloadc3p0090jar.htm

David Buck
  • 3,752
  • 35
  • 31
  • 35
Pallav Ghose
  • 81
  • 2
  • 1
0

I faced this problem but i tried that , my hibernate jar files are in a different folder.Actually , they must be in lib folder but i created a new folder called hibernate. That is wrong.You must put your jar files in the lib folder!

Doktor
  • 73
  • 2
  • 6
0

I'm sorry, it's not the same case! There is no JEE server! It's just a standalone Java Application. A small application which until now people was used to start by "java -jar -cp mysql-connector-java-8.0.27.jar xxx.jar" However this is error prone and require an installed jre! So I use jpackage, which builds a name.exe file startable as any other windows program and having all required java moduls integrated. The mysql-connector.jar is not modular and the classpath given within the configuration file of the app apparently doesn't work! So my question address people who may have experience with jpackage and eventually related issues. Beside I'm working on converting the source of the mysql-connector to modules and integrate it this way. Will probabely work, but this is for sure not exactly the idea of the inventor.

juerg
  • 381
  • 4
  • 18