创建一个javaweb项目引入如下包以及数据库驱动jar包,并创建一个spring-conf.xml配置文件
spring-conf.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 通过spring 获取属性文件中的值以供配置文件使用 -->
<bean id="SpringApplicationContext" class="com.zrar.common.ApplicationContextHelper"></bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations" value="classpath*:/application.properties"/>
</bean>
<bean id="dataSourceLocal" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的驱动-->
<property name="driverClass" value="${jdbc.driver}"/>
<!-- 指定连接数据库的URL-->
<property name="jdbcUrl" value="${jdbc.url}"/>
<!-- 指定连接数据库的用户名-->
<property name="user" value="${jdbc.username}"/>
<!-- 指定连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!-- 指定连接池中保留的最大连接数. Default:15-->
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<!-- 指定连接池中保留的最小连接数-->
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
<property name="maxStatements" value="${c3p0.maxStatements}"/>
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}"/>
</bean>
</beans>
application.properties配置如下:
#jdbc settings 配置数据库连接
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@IP:PORT:SID
jdbc.username=xxxx
jdbc.password=xxxx
#c3p0连接池配置
c3p0.minPoolSize=1
#连接池中保留的最大连接数。
c3p0.maxPoolSize=20
#初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。
c3p0.initialPoolSize=10
#最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。
c3p0.maxIdleTime=1800
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。
c3p0.acquireIncrement=10
#JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。
#但由于预缓存的statements属于单个connection而不是整个连接池。
#所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。
c3p0.maxStatements=0
#每1800秒检查所有连接池中的空闲连接。
c3p0.idleConnectionTestPeriod=1800
#定义在从数据库获取新连接失败后重复尝试的次数。
#c3p0.acquireRetryAttempts=30
#获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。
#如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。
#c3p0.breakAfterAcquireFailure=true
#因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。
#建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。
c3p0.testConnectionOnCheckout=true
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
手动获取spring的ApplicationContext和bean对象:
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Spring工具栏 * @author wan */ public class ApplicationContextHelper implements ApplicationContextAware { private static ApplicationContext appCtx; /** * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。 * @param applicationContext ApplicationContext 对象. * @throws BeansException * @author */ @Override public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException { appCtx = applicationContext; } /** * 获取ApplicationContext * @return * @author */ public static ApplicationContext getApplicationContext(){ return appCtx; } /** * 这是一个便利的方法,帮助我们快速得到一个BEAN * @param beanName bean的名字 * @return 返回一个bean对象 * @author */ public static Object getBeanById( String beanID ) { return appCtx.getBean( beanID ); } /** * 根据bean的class来查找对象 * @param c * @return */ public static Object getBeanByClass(Class c){ return applicationContext.getBean(c); } /** * 根据bean的class来查找所有的对象(包括子类) * @param c * @return */ public static Map getBeansByClass(Class c){ return applicationContext.getBeansOfType(c); } }
使用库连接:
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.datasource.DataSourceUtils; import com.mchange.v2.c3p0.ComboPooledDataSource; import ApplicationContextHelper; public class Dao{ public void test(){ ComboPooledDataSource dataSource = (ComboPooledDataSource) ApplicationContextHelper .getBean("dataSourceLocal"); Connection connection = DataSourceUtils.getConnection(dataSource); PreparedStatement state = null; ResultSet rs = null; try { state=connection.prepareStatement("select sysdate w from dual"); rs=state.executeQuery(); while (rs.next()){ System.out.println(rs.getString("w")); } } catch (SQLException e) { e.printStackTrace(); }finally{ \\关闭连接 } } }