spring中通过JNDI、DBCP、C3P0配置数据源
JNDI配置数据源
1、首先在tomcat的server.xml中配置数据源信息,找到Context,然后在里边加入如下代码
<Context docBase="SpringDemo" path="/" reloadable="true" source="org.eclipse.jst.jee.server:SpringDemo"> <Resource name="jdbc/demoDB" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@//127.0.0.1:1521/zpc" username="test" password="test" maxActive="20" maxIdle="10" maxWait="10000"/> </Context>
2、applicationContext.xml中注入bean,就这样数据源配置成功。
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/demoDB</value> </property> </bean>
DBCP配置数据源
配置applicationContext.xml中的bean
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@//127.0.0.1:1521/zpc"></property> <property name="username" value="test"></property> <property name="password" value="test"></property> <property name="initialSize" value="5"></property> </bean>
c3p0配置数据源
配置applicationContext.xml中的bean
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property> <property name="jdbcUrl" value="jdbc:oracle:thin:@//127.0.0.1:1521/zpc"></property> <property name="user" value="test"></property> <property name="password" value="test"></property> <property name="initialPoolSize" value="5"></property> </bean>
**各种方式都需要导入各自的jar包