創(chuàng)建一個javaweb項目引入如下包以及數(shù)據(jù)庫驅(qū)動jar包,并創(chuàng)建一個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"> <!-- 指定連接數(shù)據(jù)庫的驅(qū)動--> <property name="driverClass" value="${jdbc.driver}"/> <!-- 指定連接數(shù)據(jù)庫的URL--> <property name="jdbcUrl" value="${jdbc.url}"/> <!-- 指定連接數(shù)據(jù)庫的用戶名--> <property name="user" value="${jdbc.username}"/> <!-- 指定連接數(shù)據(jù)庫的密碼--> <property name="passWord" value="${jdbc.password}"/> <!-- 指定連接池中保留的最大連接數(shù). Default:15--> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/> <!-- 指定連接池中保留的最小連接數(shù)--> <property name="minPoolSize" value="${c3p0.minPoolSize}"/> <!-- 指定連接池的初始化連接數(shù) 取值應(yīng)在minPoolSize 與 maxPoolSize 之間.Default:3--> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/> <!-- 最大空閑時間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。 Default:0--> <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/> <!-- 當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù). Default:3--> <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/> <!-- JDBC的標準,用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。 但由于預(yù)緩存的statements屬于單個connection而不是整個連接池所以設(shè)置這個參數(shù)需要考慮到多方面的因數(shù). 如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。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 配置數(shù)據(jù)庫連接jdbc.driver=Oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@ip:PORT:SIDjdbc.username=xxxxjdbc.password=xxxx#c3p0連接池配置c3p0.minPoolSize=1#連接池中保留的最大連接數(shù)。c3p0.maxPoolSize=20#初始化時獲取三個連接,取值應(yīng)在minPoolSize與maxPoolSize之間。c3p0.initialPoolSize=10#最大空閑時間,1800秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。c3p0.maxIdleTime=1800#當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù)。c3p0.acquireIncrement=10#JDBC的標準參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。#但由于預(yù)緩存的statements屬于單個connection而不是整個連接池。#所以設(shè)置這個參數(shù)需要考慮到多方面的因素。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。c3p0.maxStatements=0#每1800秒檢查所有連接池中的空閑連接。c3p0.idleConnectionTestPeriod=1800#定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)。#c3p0.acquireRetryAttempts=30#獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效保留,并在下次調(diào)用getConnection()的時候繼續(xù)嘗試獲取連接。#如果設(shè)為true,那么在嘗試獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關(guān)閉。#c3p0.breakAfterAcquireFailure=true#因性能消耗大請只在需要的時候使用它。如果設(shè)為true那么在每個connection提交的時候都將校驗其有效性。#建議使用idleConnectionTestPeriod或automaticTestTable等方法來提升連接測試的性能。c3p0.testConnectionOnCheckout=trueweb.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到當前類中作為一個靜態(tài)成員變量。 * @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 ); } /** * 根據(jù)bean的class來查找對象 * @param c * @return */ public static Object getBeanByClass(Class c){ return applicationContext.getBean(c); } /** * 根據(jù)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{ //關(guān)閉連接 } }}
新聞熱點
疑難解答