我正在尝试运行此示例http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/PoolingDataSourceExample.java?view=markup,但不知怎的,我无法做到这一点。我想在maven项目中运行这个例子,我也是第一次在maven项目上工作。我不知道该怎么做。在那个例子中,它明确指出 -
To compile this example, you'll want:
* commons-pool-1.5.4.jar
* commons-dbcp-1.2.2.jar
* j2ee.jar (for the javax.sql classes)
in your classpath.
To run this example, you'll want:
* commons-pool-1.5.6.jar
* commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)
* j2ee.jar (for the javax.sql classes)
* the classes for your (underlying) JDBC driver
in your classpath.
所以我在完成各种文章之后尝试将所有这些依赖项添加到我的pom.xml文件中。但我仍然有像 -
这样的错误ConnectionFactory cannot be resolved to a type
GenericObjectPool cannot be resolved to a type
ObjectPool cannot be resolved to a type
PoolableConnectionFactory cannot be resolved to a type
PoolableConnectionFactory cannot be resolved to a type
PoolingDataSource cannot be resolved to a type
PoolingDataSource cannot be resolved to a type
这是我的pom.xml文件。我的pom.xml文件中是否有任何遗漏的内容。任何建议都将受到赞赏,因为这是我第一次使用maven项目。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.datasource.pooling</groupId>
<artifactId>datasource.pooling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.datasource.pooling</name>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
</dependency>
-->
</dependencies>
</project>
添加j2ee.jar文件的依赖性是什么。
答案 0 :(得分:3)
ConnectionFactory,GenericObjectPool,ObjectPool,PoolableConnectionFactory,PoolingDataSource类可以在jars commons-pool- {version} .jar和commons-dbcp- {version} .jar中找到。
在这种情况下,您不必添加j2ee.jar。 javax.sql包相关的类在rt.jar中,默认在jdk中。
您必须运行的代码(http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/PoolingDataSourceExample.java?view=markup)是从包org.apache.commons.dbcp2
和org.apache.commons.pool2
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
改为使用
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
在所有包导入中使用org.apache.commons.pool
而不是org.apache.commons.pool2
。
这些类位于 commons-pool- {version} .jar 和 commons-dbcp- {version} .jar jar中。