spring 配置xml连接数据库,出现 java.lang.NoClassDefFoundError: Lorg/apache/commons/pool/impl/GenericObjectPool

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- PropertyPlaceholderConfigurer 使用外部属性文件 -->
	<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

	<bean id="dbSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driverClassName}" />
		<property name="url" value="${url}" />
		<property name="username" value="${user}" />
		<property name="password" value="${password}" />
	</bean>
</beans>
db.properties外部属性文件

user=lm
password=lm
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
java类

package com.test.main;

import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcMain_1 {

	public static void main(String[] args) throws SQLException {

		ApplicationContext ac = new ClassPathXmlApplicationContext("outerproperty.xml");
		BasicDataSource ds = (BasicDataSource) ac.getBean("dbSource");
		System.out.println(ds.getConnection());

	}
}
当执行住方法时就会出现

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbSource' defined in class path resource [outerproperty.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Lorg/apache/commons/pool/impl/GenericObjectPool;
的问题

参照网上别的博主的博客将commons.pool.jar加载到classpath(其实这个jar包eclipse的plugins文件夹下有的)但还是会出现同样的问题,然后找了半天还是不知道问题在哪儿,最后我又重新下了一个版本为1.5的commons.pool.jar包(以前是1.6的)下载网址为http://maven.ibiblio.org/maven2/commons-pool/commons-pool/1.5/,然后就没出现这个问题了,那就应该时版本不适合的问题了。但是接着出现了下面的问题:

Exception in thread "main" org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied。


然后就通过System.out.println(ds.getUserName())将用户名打印下来,结果不是我配置的属性文件的用户名而是我电脑的名字。然后我把属性文件username属性改为user,

再打印下来就是对应属性的用户名了,然后问题就解决了。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值