driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
username=root
password=123456
<!--
--------PropertyOverrideConfigurer ------------------
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/spring
dataSource.user=root
dataSource.password=123456 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!--<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dataSource.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${driverClassName}" p:jdbcUrl="${url}" p:user="${username}" p:password="${password}"
/>
</beans>
package com.ij34.bean;
import java.sql.*;
import javax.sql.DataSource; import org.springframework.context.*;
import org.springframework.context.support.*; public class test { public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
DataSource ds=(DataSource) ac.getBean("dataSource");
Connection conn=ds.getConnection();
PreparedStatement ps= conn.prepareStatement("insert into news_inf value(null,?,?)");
ps.setString(1, "加油2017");
ps.setString(2, "2017,这是挑战与机遇的一年!");
ps.executeUpdate();
ps.close();
conn.close();
}
}