commons-dbcp-1.4.jar +commons-collections4-4.0.jar+commons-pool2-2.0.jar
错误代码
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
at org.apache.commons.dbcp.BasicDataSourceFactory.createDataSource(BasicDataSourceFactory.java:167)
at com.jdbcUtils.dbpcUtils.<clinit>(dbpcUtils.java:27)
at com.dbcp.base.main(base.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool.KeyedObjectPoolFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
源码:dbpcUtils.java
package com.jdbcUtils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class dbpcUtils {
private static DataSource myDataSource = null;
public dbpcUtils() {}
static{
try {
Properties prop = new Properties();
InputStream is = dbpcUtils.class.getClassLoader()
.getResourceAsStream("dbcpconfig.properties");
System.out.println(is);
prop.load(is);
myDataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource() {
return myDataSource;
}
public static Connection getConnection() throws SQLException {
return myDataSource.getConnection();
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}}
源码:base.java
package com.dbcp;
import java.sql.Connection;
import java.sql.SQLException;import com.jdbcUtils.dbpcUtils;
public class base {
public static void main(String[] args) throws SQLException {
for (int i = 0; i < 10; i++) {
Connection conn = dbpcUtils.getConnection();
System.out.println(conn.getClass().getName());
dbpcUtils.free(null, null, conn);
}
}
}
你看看这几个包里有org/apache/commons/pool/KeyedObjectPoolFactory 这个类么
commons-pool-1.5.4.jar 这个包里面有提示里缺失的那个类