这是spring配置代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:proxool-jdbc.properties</value>
</list>
</property>
</bean>
<bean id="localDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
<property name="driver">
<value>${proxool.local.driver}</value>
</property>
<property name="driverUrl">
<value>${proxool.local.url}</value>
</property>
<property name="user">
<value>${proxool.local.username}</value>
</property>
<property name="password">
<value>${proxool.local.password}</value>
</property>
<property name="alias">
<value>${proxool.local.alias}</value>
</property>
<property name="houseKeepingSleepTime" value="30000" />
<property name="maximumActiveTime" value="10000" />
<property name="prototypeCount" value="5" />
<property name="maximumConnectionCount" value="50" />
<property name="minimumConnectionCount" value="10" />
</bean>
</beans>
java代码: DataSource工厂类
public class DataSourceFactory {
private static Logger logger = Logger.getLogger(DataSourceFactory.class);
private static BeanFactory beanfactory = null;
static {
if (beanfactory == null)
beanfactory = new FileSystemXmlApplicationContext(
"classpath:applicationContext.xml");
}
public static synchronized DataSource getLocalDataSource() {
return (DataSource) beanfactory.getBean("localDataSource");
}
}
获取连接类。
public class DBConnectionFactory {
public static synchronized Connection getLocalDBConnection()
throws SQLException {
Connection connection = null;
try {
connection = DataSourceFactory.getLocalDataSource().getConnection();
connection.setAutoCommit(false);
} catch (SQLException sqle) {
sqle.printStackTrace();
throw new SQLException("数据库连接失败!");
}
if (connection == null) {
throw new SQLException(
"没有创建任何数据库连接!");
} else {
return connection;
}
}
}
然后,业务代码在service层调用。
private Connection conn = null;
public List getMsgRecord(){
try{
conn = DBConnectionFactory.getLocalDBConnection();
List msgList = recordDao.getRecord(conn);
} finally {
if (null != conn) {
conn.close();
}
}
}
getRecord方法。
代码
List list = new ArrayList();
PreparedStatement pstmt = null;
try{
ResultSet rs = conn.prepareStatement("select * from tableA");
while (rs.next()) {
CommonMsgEntity msgEntity = new CommonMsgEntity();
msgEntity.setSRI_ID(rs.getInt("SRI_ID") + "");
msgEntity.setSRI_APPNAME(rs.getString("SRI_APPNAME"));
msgEntity.setSRI_TYPE(rs.getInt("SRI_TYPE") + "");
msgEntity.setSRI_TITLE(rs.getString("SRI_TITLE"));
msgEntity.setSRI_URL(rs.getString("SRI_URL"));
msgEntity.setSRI_RECTIME(rs.getString("SRI_RECTIME"));
msgEntity.setSRI_APPTIME(rs.getString("SRI_APPTIME"));
list.add(msgEntity);
}
return list;
}finally {
try {
if (null != rs) {
rs.close();
}
if (null != pstmt) {
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}