1:applicationcontext.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"
Xmlns:util= "Http://www.springframework.org/schema/util"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:p= "http://www.springframework.org/schema/p"
Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"
Xsi:schemalocation= "Http://www.springframework.org/schema/jdbc
Http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
<!--importing resource files--
<context:property-placeholder location= "Classpath:db.properties"/>
<!--configuring C3P0 data Sources--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "User" ><value>root</value></property>
<property name= "Password" ><value>123456</value></property>
<property name= "Jdbcurl" ><value>jdbc:mysql://localHost/mydatabase</value></property>
<property name= "Driverclass" ><value>com.mysql.jdbc.Driver</value></property>
<property name= "Initialpoolsize" ><value>5</value></property>
<property name= "Minpoolsize" ><value>5</value></property>
<property name= "Maxpoolsize" ><value>10</value></property>
</bean>
</beans>
2:db.properties
Username=root
password=123456
Jdbcurl=jdbc:mysql://localhost/mydatabase
Driverclass=com.mysql.jdbc.driver
Minpoolsize=5
Initialpoolsize=5
Maxpoolsize=15
3:junit
Package com.spring.jdbc;
Import static org.junit.assert.*;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import org.junit.Test;
import org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Jdbctest {
private applicationcontext ctx=null;
{
ctx=new CLASSP Athxmlapplicationcontext ("Applicationcontext.xml");
}
@Test
public void Test () throws SQLException {
DataSource datasource=ctx.getbean (datasource.class);
System.out.println (Datasource.getconnection ());
}
}