1
我需要你的幫助,我的代碼。我有這個代碼PoolConnector類:org.apache.tomcat.jdbc.pool.DataSource missing
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.jdbc.pool.PoolProperties;)
我做錯了什麼? 預先感謝您。
謝謝你,我在我的classpath中添加它,現在一切正常:) – user3451793 2015-02-09 19:36:48
添加tomcat-jdbc.jar解決了我的問題。雖然這個答案給了我這個想法,所以我正在做+1。 – 2017-01-28 17:18:09