org.apache.tomcat.jdbc.pool.DataSource 失踪

org.apache.tomcat.jdbc.pool.DataSource missing

我的代码需要你的帮助。我有一个 PoolConnector class,代码如下:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class PoolConnector

{

private static final String user = "root";
private static final String password = "";
private static final String dbUrl = "jdbc:mysql://localhost/gene_ontology";
private static DataSource ds;

static {
    try {
        Context context = new InitialContext();
        Context envctx =  (Context) context.lookup("java:comp/env");
        ds =  (DataSource) envctx.lookup("jdbc/TestDB");
    } 
    catch (NamingException ex) {
        Logger.getLogger(PoolConnector.class.getName()).log(Level.SEVERE, null, ex);
        PoolProperties p = new PoolProperties();
        p.setUrl("jdbc:mysql://localhost/gene_ontology?autoReconnect=true");
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername("root");
        p.setPassword("");
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(100);
        p.setInitialSize(10);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(60);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(10);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        ds = new DataSource();
        ds.setPoolProperties(p);
    }
}

public static Connection getConnection()
{
    Connection conn = null;
    try 
    {    
        conn =
                DriverManager.getConnection(dbUrl, user, password);
    }

    catch (SQLException e)
    {
        e.printStackTrace();
    }
    finally 
    {
        return conn;
    }
}

public static Connection getConnection(boolean pool)
{
    if (pool)
    {
        Connection conn = null;
        try
        {

            conn = ds.getConnection();
            return conn;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }
    else return getConnection();
}

public static boolean closeConnection(Connection conn)
{
    try 
    {
        conn.close();
        return true;
    } 
    catch (SQLException ex) 
    {
        Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, 
                "Connection could not be closed", ex);
        return false;
    }
}

}

我收到两个包的错误,它们似乎不存在 (导入org.apache.tomcat.jdbc.pool.DataSource; 导入 org.apache.tomcat.jdbc.pool.PoolProperties;)

我做错了什么? 提前谢谢你。

你的类路径中有 tomcat-dbcp.jar 吗?请确保 tomcat lib 目录在您的类路径中。