ab935a03814c537668f6cfa3fd6042f6de6d8d65.svn-base 3.9 KB

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