import javax.sql.DataSource; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.orm.hibernate3.HibernateJdbcException; import com.vtradex.thorn.server.exception.BusinessException; /** * @description 获得数据源工厂类 */ public class DataSourceFactory implements ApplicationContextAware{ private static ApplicationContext ac; public static DataSource getDataSourceFactory(String dataSourceName){ DataSource ds = null; try{ ds = (DataSource)ac.getBean(dataSourceName); }catch(NoSuchBeanDefinitionException e) { throw new BusinessException("dataSource.dataBase.unActive", new String[]{dataSourceName}); }catch(HibernateJdbcException ex){ throw new BusinessException("dataSource.dataBase.connect.erro", new String[]{dataSourceName}); } return ds; } public void setApplicationContext(ApplicationContext ac) throws BeansException { DataSourceFactory.ac = ac; } }
/**使用案例*/ public void execute(){ Connection connection = null; PreparedStatement pStatement = null;// PreparedStatement PreparedStatement upStatement = null; ResultSet resultSet = null;// ResultSet try{ connection = getConnection(); connection.setAutoCommit(Boolean.FALSE); pStatement = connection.prepareStatement("select name from a"); resultSet = pStatement.executeQuery(); while(resultSet.next()){ String name = resultSet.getString("name"); } java.sql.Timestamp timestamp = new java.sql.Timestamp(new Date().getTime()); // 回写中间表信息 String updateMess = "UPDATE " + MiddleTableName.ORDERACCEPTNAME + " SET STATUS = ?, EXCEPTION_MESS = ?,BACK_TIME=? WHERE ID = ?"; upStatement = connection.prepareStatement(updateMess); if (haveExce) { if ("NO_EXIST".equals(errerType)) { upStatement.setInt(1, 4); } else { upStatement.setInt(1, 0); } } else { upStatement.setInt(1, 3); } upStatement.setString(2, exceDesc); upStatement.setTimestamp(3,timestamp); upStatement.setLong(4, id); upStatement.execute(); connection.commit(); if (null != upStatement) { upStatement.close(); } }catch(Exception e) { System.out.println(e.getLocalizedMessage()); try { connection.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } }finally{ free(resultSet, pStatement, connection); } } public Connection getConnection() { Connection conn = null; DataSource ds = DataSourceFactory.getDataSourceFactory("middleTble"); try { conn = ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; } //关闭资源 public void free(ResultSet resultSet, Statement statement, Connection connection) { if (null != resultSet) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (null != connection) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } }else{ if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (null != connection) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } }else{ try { if (null != connection) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
数据源配置文件xml
<?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="middleTble" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>oracle.jdbc.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@192.168.12.111:1521:orcl</value> </property> <property name="username"> <value>middleTable</value> </property> <property name="password"> <value>middleTable</value> </property> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="30"/> <!-- 连接池的最大值 --> <property name="maxActive" value="500"/> <!-- 最大空闲值.当经过一个高峰时间后, 连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2"/> <!-- 最小空闲值.当空闲的连接数少于阀值时, 连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1"/> </bean> </beans>
相关推荐
1,利用java调用soap接口获取天气的信息;2,利用java调用soap一个特定的案例,可按自己的要求修改。
PHP开发APP接口案例(21讲)PHP开发APP接口案例(21讲)PHP开发APP接口案例(21讲)
微服务项目实战微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目实战案例分享微服务项目...
java调用webservice接口案例,精简,service调用webservice接口案例;不用生成一堆代码,逻辑清晰
Java ---- 接口案例
SAP ABAP 调用外部接口webservice案例分享,含接口配置及ABAP调用代码
这是一个银联控件 app端用的案例,已经测试没问题,大家可根据自己环境来配置。
银联接口案例、仅是学习所用的,本人也是学习中去尝试的,希望可以帮到大家。
Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享Rust语言教程和案例分享...
2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口...
Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享Ruby语言教程及案例分享...
C#接口实现案例 c#经典案例.pdf
接口使用,接口书上的使用案例,简单易懂。
作品登记表(融合创新应用教学案例、教师网络空间应用案例、信息化教学课程案例)分享.pdf
大数据案例分享
微信接口开发公众号开发经验分享——接口开发案例代码.docx
backtrader回测功能强大,但一直缺乏针对中国ctp期货的实盘接口。现在,我们开发了此接口,并提供您可运行的案例源码,请下载体验backtrader中国期货实盘仿真交易的乐趣。
支付宝快速付款接口案例下载支付宝快速付款接口案例下载支付宝快速付款接口案例下载
接口編程的經典案例------結構體切片案例
vue项目实战基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例分享基于Vue.js的实战案例...