首页 新闻 会员 周边 捐助

java 数据源

0
[已解决问题] 解决于 2014-07-29 16:20

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);
    }
  }
}

菜鸟&努力的主页 菜鸟&努力 | 初学一级 | 园豆:196
提问于:2013-11-26 19:26
< >
分享
最佳答案
-1

你看看这几个包里有org/apache/commons/pool/KeyedObjectPoolFactory 这个类么

 

commons-pool-1.5.4.jar 这个包里面有提示里缺失的那个类

奖励园豆:5
woody.song | 菜鸟二级 |园豆:207 | 2014-02-04 16:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册