gpt4 book ai didi

java - 缺少 org.apache.tomcat.jdbc.pool.DataSource

转载 作者:行者123 更新时间:2023-11-29 05:11:11 27 4
gpt4 key购买 nike

我的代码需要你的帮助。我有一个带有此代码的 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;导入 org.apache.tomcat.jdbc.pool.PoolProperties;)

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

最佳答案

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

关于java - 缺少 org.apache.tomcat.jdbc.pool.DataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28417479/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com