javax.servlet.ServletException:java.lang.NoClassDefFoundError:org / apache / commons / pool / KeyedObjectPoolFactory [英] javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory

查看:146
本文介绍了javax.servlet.ServletException:java.lang.NoClassDefFoundError:org / apache / commons / pool / KeyedObjectPoolFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 BasicDataSource 使用 DBCP

我下载了 commons-dbcp-1.4-bin.zip /dbcp/download_dbcp.cgirel =nofollow>此处。

类路径上有三个jar文件。

There are three jar files on the classpath.


  1. commons-dbcp-1.4

  2. commons-dbcp-1.4-sources

  3. commons-dbcp-1.4-javadoc

  1. commons-dbcp-1.4
  2. commons-dbcp-1.4-sources
  3. commons-dbcp-1.4-javadoc

我使用以下代码建立连接。

I'm using the following code for the connection to be established.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

public class DatabaseConnection {

    private final static BasicDataSource BASIC_DATA_SOURCE = new BasicDataSource();
    private final static String SQL = "SELECT * FROM admin WHERE login_id=? AND admin_pwd=?";

    static {
        BASIC_DATA_SOURCE.setDriverClassName("oracle.jdbc.OracleDriver");
        BASIC_DATA_SOURCE.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        BASIC_DATA_SOURCE.setUsername("wagafashiondb");
        BASIC_DATA_SOURCE.setPassword("root");
    }

    private static Connection getConnection() throws SQLException {
        return BASIC_DATA_SOURCE.getConnection();
    }

    public boolean exists(String userName, String password) throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean exists = false;

        try {
            connection = getConnection();
            ps = connection.prepareStatement(SQL);
            ps.setString(1, userName);
            ps.setString(2, password);
            rs = ps.executeQuery();
            exists = rs.next();
        } finally {
            try {
                if (connection != null) {connection.close();}
                if (ps != null) {ps.close();}
                if (rs != null) {rs.close();}
            } catch (SQLException e) {

            }
        }
        return exists;
    }
}

它会引发以下异常:

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory

这似乎暗示我使用错误的来源。

It appears to imply that I'm using a wrong source. What might be the reason for this exception to be caused?

推荐答案

解决方案

您需要在类路径上获取 commons-pool 库(JAR)。
这个事实表示为 commons-pool commons-dbcp http://commons.apache.org/dbcp/dependencies.html\">项目的依赖列表

You need to get the commons-pool library (JAR) on a classpath. The fact is indicated as commons-pool being a dependency of commons-dbcp in project's dependecies list

说明

基本上, NoClassDefFoundError 意味着Java执行结果需要一个不在类路径上的特定类。这是因为类需要其他类(通常在 import 中指示)。在你的情况下(至少) commons-dbcp 中的一个类声明了对 org / apache / commons / pool / KeyedObjectPoolFactory

Basically the NoClassDefFoundError means the Java execution turned out to require a specific class which is not on classpath. This is because classes needs other classes (typically indicated in imports). In your case (at least) one of the classes from commons-dbcp declares a dependency to org/apache/commons/pool/KeyedObjectPoolFactory.

类的名称使用 / 作为分隔符。如果这是一个通用名字(不是你自己的类),你可以尝试通过输入完整的名称来找到它在哪里得到它(例如 org / apache / commons / pool / KeyedObjectPoolFactory )。

The name of the class is given using / as separators. If that's a common name (not your own class), you may try to find where to get it from by just typing the full name (e.g. org/apache/commons/pool/KeyedObjectPoolFactory) in Google.

这篇关于javax.servlet.ServletException:java.lang.NoClassDefFoundError:org / apache / commons / pool / KeyedObjectPoolFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆