日期:2014-05-18  浏览次数:20671 次

请问如何把SPRING中的连接数据库信息封装到JAVABEAN中?用到了连接池c3p0
为了安全,以前没用到连接池是这样作的,现在用com.mchange.v2.c3p0.ComboPooledDataSource,不知如何是好了?
<bean   id= "dataSource "   class= "com.icontrol.init.DataSource ">
</bean>
public   class   DataSource   extends   DriverManagerDataSource
{
        public   DataSource()
        {
this.setDriverClassName( "com.mysql.jdbc.Driver ");
this.setUrl( "jdbc:mysql://localhost:3306/db ");
this.setUsername( "root ");
                  this.setPassword( "123456 ");
        }
}

------解决方案--------------------
public class MyDataSource implements org.springframework.beans.factory.FactoryBean {

private ComboPooledDataSource dataSource;

public MyDataSource() throws PropertyVetoException {
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass( " ");
dataSource.setJdbcUrl( " ");
dataSource.setUser( " ");
dataSource.setPassword( " ");
}

public Object getObject() throws Exception {
return dataSource;
}

public Class getObjectType() {
return dataSource.getClass();
}

public boolean isSingleton() {
return true;
}
}

------解决方案--------------------
<!--
配置数据源
注意: 用org.apache.commons.dbcp.BasicDataSource, 要引入 apache commons
的commons-collections-3.1.jar, commons-dbcp-1.2.1.jar, commons-pool-1.2.jar三个包
-->
<bean id= "dataSource " class= "org.apache.commons.dbcp.BasicDataSource "
destroy-method= "close ">
<property name= "driverClassName ">
<!-- <value> oracle.jdbc.OracleDriver </value> -->
<value> oracle.jdbc.driver.OracleDriver </value>
</property>
<property name= "url ">
<value> jdbc:oracle:thin:@192.168.1.0:1521:sinitek </value>
</property>
<property name= "username ">
<value> test </value>
</property>
<property name= "password ">
<value> test </value>
</property>
</bean>

<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
<bean id= "sessionFactory "
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name= "dataSource ">
<ref local= "dataSource "/>
</property>
<property name= "mappingResources ">
<list>
<value> test/struts/Regiest.hbm.xml </value>
</list>
</property>
<property name= "hibernateProperties ">
<props>
<prop key= "hibernate.dialect ">
org.hibernate.dialect.OracleDialect
</prop>
<prop key= "hibernate.show_sql "> true </prop>
<!-- 设置c3p0连接池 -->
<!-- <prop key= "hibernate.c3p0.minPoolSize "> 0 </prop>
<prop key= "hibernate.c3p0.maxPoolSize "> 5 </prop>
<prop key= "hibernate.c3p0.timeout "> 600 </prop>
<prop key= "h