cc7521a5aea45e7e94396557551f7f0a4f6511e1.svn-base 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package QCM.COMMUNAL;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import org.apache.commons.dbcp.BasicDataSource;
  9. import CoreFS.SA01.CoreIComponent;
  10. import CoreFS.SA06.CoreReturnObject;
  11. import CoreFS.SA06.CoreSqlType;
  12. import UIB.COM.XmlSqlParsersFactory;
  13. /**
  14. *
  15. * @desc 存储过程调用
  16. * @author meiguiping
  17. * @date 2010 1:17:52 PM
  18. */
  19. public class ComDBProcedureQCM extends CoreIComponent
  20. {
  21. /**
  22. * @desc 执行存储过程
  23. * @param sqlID xml中的SQL的ID
  24. * @param ht 前台需要用hashtable传入,所有参数均存入hashmap中 ,前台必须以(I1,""),(I2,""),,(O1,""),,(O2,"")的方式存入
  25. * I表示in,O表示out。
  26. * @return CoreReturnObject
  27. * @throws Exception
  28. */
  29. public CoreReturnObject executeProcedure(String sqlID , HashMap ht ) throws Exception
  30. {
  31. CoreReturnObject cro = new CoreReturnObject();
  32. ArrayList inParam = new ArrayList();
  33. ArrayList inParamType = new ArrayList();
  34. ArrayList aList = new ArrayList();
  35. String key = "";
  36. ArrayList outParam = new ArrayList();
  37. ArrayList outparamtype = new ArrayList();
  38. String[] str = new String[3];
  39. int index = 0;
  40. for(Iterator it = ht.keySet().iterator(); it.hasNext(); )
  41. {
  42. key = it.next().toString();
  43. aList.add(key);
  44. }
  45. Collections.sort(aList);
  46. for(int i=0; i < aList.size(); i++)
  47. {
  48. if(aList.get(i).toString().toLowerCase().startsWith("i"))
  49. {
  50. inParam.add(ht.get(aList.get(i).toString()));
  51. inParamType.add(new Integer(CoreSqlType.CoreOracleType.STRING_TYPE.ordinal()));
  52. }
  53. else if (aList.get(i).toString().toLowerCase().startsWith("o"))
  54. {
  55. outParam.add(str[index++]);
  56. outparamtype.add(new Integer(CoreSqlType.CoreOracleType.STRING_TYPE.ordinal()));
  57. }
  58. }
  59. ht = null;//将HashMap置空,防止内存泄露
  60. String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
  61. cro = this.getDao("test1Dao").ExcuteProcedure(sqlString, inParamType , inParam , outparamtype , outParam);
  62. return cro;
  63. }
  64. /**
  65. * @param sqlID
  66. * @param ht
  67. * @return
  68. * @throws Exception
  69. */
  70. public CoreReturnObject doXmlProcedure(String sqlID , HashMap ht ) throws Exception
  71. {
  72. CoreReturnObject cro = new CoreReturnObject();
  73. if(ht == null) return cro;
  74. String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
  75. Connection conn = this.getDao("test1Dao").getConnection();
  76. CallableStatement cstm = conn.prepareCall(sqlString);
  77. String key = "";
  78. // BasicDataSource ds = (BasicDataSource)this.dbproxy.getJdbcTemplate().getDataSource();
  79. try
  80. {
  81. String[] outIndex = new String[3];//最多为3个输出
  82. int j = 0;
  83. for(Iterator it = ht.keySet().iterator(); it.hasNext(); )
  84. {
  85. key = it.next().toString();
  86. if(key.toLowerCase().startsWith("i"))
  87. {
  88. cstm.setString(Integer.parseInt(key.substring(1)), ht.get(key).toString());
  89. }
  90. else if(key.toLowerCase().startsWith("o"))
  91. {
  92. cstm.registerOutParameter(Integer.parseInt(key.substring(1)), java.sql.Types.VARCHAR);
  93. outIndex[j++] = key.substring(1);
  94. }
  95. }
  96. ht = null;//将HashMap置空,防止内存泄露
  97. if(cstm == null) throw new Exception("获取存储过程出错,请检查!");
  98. cstm.execute();
  99. ArrayList aList = new ArrayList();
  100. //输出
  101. for(int i = 0; i < outIndex.length; i++)
  102. {
  103. if( outIndex[i] != null)
  104. {
  105. aList.add(cstm.getString(Integer.parseInt(outIndex[i])));
  106. // System.out.print("************************输出: "+cstm.getString(Integer.parseInt(outIndex[i])));
  107. }
  108. }
  109. cro.setResult(aList);
  110. }catch(Exception ex)
  111. {
  112. ex.printStackTrace();
  113. }
  114. finally
  115. {
  116. if(cstm != null)
  117. cstm.close();
  118. if(conn != null)
  119. conn.close();
  120. // System.out.println("关闭后,连接池最大连接数========================>"+ds.getMaxIdle());
  121. // System.out.println("关闭后,当前活动连接数#########################"+ds.getNumActive());
  122. // System.out.println("关闭后,当前连接池连接数#########################"+ds.getNumIdle());
  123. }
  124. return cro;
  125. }
  126. }