代码之家  ›  专栏  ›  技术社区  ›  user3451793

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

  •  1
  • user3451793  · 技术社区  · 9 年前

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

    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; import org.apache.tomcat.jdb.pool.PoolProperties;)

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

    1 回复  |  直到 9 年前
        1
  •  3
  •   erhun    9 年前

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