我对MySQL比较了解,我正在尝试制作一个访问MySQL数据库的程序。我目前正在尝试使用C3P0和mchange-commons API通过数据源访问它。每当我编译并运行程序时,都会出现以下错误:
java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource
在使用以下代码创建ComboPooledDataSource的行上发生错误:
public DataSource getDataSource() {
return dataSource("com.mysql.jdbc.Driver", "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?useSSL=false", this.username, this.password);
}
public ComboPooledDataSource dataSource(String driver, String url, String username, String password) {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource(); // This is the line with the error.
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setAcquireIncrement(1);
dataSource.setMaxPoolSize(100);
dataSource.setMinPoolSize(1);
dataSource.setInitialPoolSize(1);
dataSource.setMaxIdleTime(300);
dataSource.setMaxConnectionAge(36000);
dataSource.setAcquireRetryAttempts(5);
dataSource.setAcquireRetryDelay(2000);
dataSource.setBreakAfterAcquireFailure(false);
dataSource.setCheckoutTimeout(30000);
dataSource.setPreferredTestQuery("SELECT 1");
dataSource.setIdleConnectionTestPeriod(60);
return dataSource;
} catch (PropertyVetoException ex) {
ex.printStackTrace();
return null;
}
}
我没有使用Maven将API添加到程序中。我正在使用IntelliJ IDEA并将两个jar文件添加为外部库。也许我做错了。如果是我,请告诉我。