110ff8c3780578ed2ea6b3675efa201b02f79cce.svn-base 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package UIB.JHY;
  2. import CoreFS.SA01.CoreIComponent;
  3. import CoreFS.SA06.CoreReturnObject;
  4. import CoreFS.SA06.CoreSqlType.CoreOracleType;
  5. import UIB.COM.XmlSqlParsersFactory;
  6. import oracle.sql.ARRAY;
  7. import oracle.sql.ArrayDescriptor;
  8. import org.apache.commons.dbcp.DelegatingConnection;
  9. import java.sql.Array;
  10. import java.sql.CallableStatement;
  11. import java.sql.Connection;
  12. import java.sql.SQLException;
  13. import java.util.*;
  14. public class ComCallProc extends CoreIComponent {
  15. /**
  16. * @param sqlID xml中的SQL的ID
  17. * @param ht 前台需要用hashtable传入,所有参数均存入hashmap中 ,前台必须以(I1,""),(I2,""),,(O1,""),,(O2,"")的方式存入
  18. * I表示in,O表示out。
  19. * @return CoreReturnObject
  20. * @throws Exception
  21. * @desc 执行存储过程
  22. */
  23. public CoreReturnObject doCoreProc(String dao, String sqlID, HashMap ht) throws Exception {
  24. CoreReturnObject cro = new CoreReturnObject();
  25. ArrayList inParam = new ArrayList();//输入参数
  26. ArrayList inParamType = new ArrayList();//输入参数类型
  27. ArrayList aList = new ArrayList();
  28. String key = "";
  29. ArrayList outParam = new ArrayList();//输出参数
  30. ArrayList outparamtype = new ArrayList();//输出参数类型
  31. String[] str = new String[3];
  32. int index = 0;
  33. //取出hashmap中的key,存储进ArrayList中
  34. for (Iterator it = ht.keySet().iterator(); it.hasNext(); ) {
  35. key = it.next().toString();
  36. aList.add(key);
  37. }
  38. Collections.sort(aList);
  39. String key_1 = null;
  40. for (int i = 0; i < aList.size(); i++) {
  41. key_1 = aList.get(i).toString().toLowerCase();
  42. if (key_1.startsWith("i"))//输入参数
  43. {
  44. if (key_1.indexOf(":") != -1) {
  45. ArrayList al = null;
  46. Object obj = ht.get(aList.get(i).toString());//是否按ArrayList传递
  47. if (obj instanceof ArrayList) {
  48. al = (ArrayList) obj;
  49. } else //进行转换
  50. {
  51. al = new ArrayList();
  52. al.add(obj.toString());
  53. }
  54. Array ar = this.getDao(dao).getArray(key_1.substring(key_1.indexOf(":") + 1).toUpperCase(), al);
  55. inParam.add(ar);
  56. inParamType.add(CoreOracleType.ARRAY_TYPE.ordinal());
  57. } else//输出参数
  58. {
  59. inParam.add(ht.get(aList.get(i).toString()));
  60. inParamType.add(new Integer(CoreOracleType.STRING_TYPE.ordinal()));
  61. }
  62. } else if (key_1.startsWith("o")) {
  63. //outParam.add(str[index++]);
  64. // outparamtype.add(new Integer(CoreSqlType.CoreOracleType.STRING_TYPE.ordinal()));
  65. outparamtype.add(CoreOracleType.STRING_TYPE.ordinal());
  66. }
  67. }
  68. ht = null;
  69. String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
  70. cro = this.getDao(dao).ExcuteProcedure(sqlString, inParamType, inParam, outparamtype, outParam);
  71. // cro = this.getDao(dao).ExcuteProcedure(sqlString, inParamType , inParam );
  72. return cro;
  73. }
  74. /**
  75. * @param sqlID
  76. * @param ht
  77. * @return
  78. * @throws Exception
  79. */
  80. public CoreReturnObject doSimpleProc(String dao, String sqlID, HashMap ht) throws Exception {
  81. CoreReturnObject cro = new CoreReturnObject();
  82. if (ht == null) return cro;
  83. String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
  84. Connection conn = this.getDao(dao).getConnection();
  85. CallableStatement cstm = conn.prepareCall(sqlString);
  86. String key = "";
  87. // BasicDataSource ds = (BasicDataSource)this.dbproxy.getJdbcTemplate().getDataSource();
  88. try {
  89. String[] outIndex = new String[3];//最多为3个输出
  90. int j = 0;
  91. for (Iterator it = ht.keySet().iterator(); it.hasNext(); ) {
  92. key = it.next().toString();
  93. if (key.toLowerCase().startsWith("i")) {
  94. if (key.toLowerCase().indexOf(":") > 0)//":"不会在字符串的最前面,判断是否包含“:”
  95. {
  96. String arrayType = key.substring(key.indexOf(":") + 1).toUpperCase();//数组类型
  97. ArrayDescriptor ad = ArrayDescriptor.createDescriptor(arrayType, getNativeConnection(conn));
  98. ARRAY array = new ARRAY(ad, getNativeConnection(conn), ((List) ht.get(key)).toArray());
  99. cstm.setArray(Integer.parseInt(key.substring(1, 2)), array);
  100. } else {
  101. cstm.setString(Integer.parseInt(key.substring(1)), ht.get(key).toString());
  102. }
  103. } else if (key.toLowerCase().startsWith("o")) {
  104. cstm.registerOutParameter(Integer.parseInt(key.substring(1)), java.sql.Types.VARCHAR);
  105. outIndex[j++] = key.substring(1);
  106. }
  107. }
  108. ht = null;
  109. if (cstm == null) throw new Exception("获取存储过程出错,请检查!");
  110. cstm.execute();
  111. ArrayList aList = new ArrayList();
  112. //输出
  113. for (int i = 0; i < outIndex.length; i++) {
  114. if (outIndex[i] != null) {
  115. aList.add(cstm.getString(Integer.parseInt(outIndex[i])));
  116. }
  117. }
  118. cro.setResult(aList);
  119. } catch (Exception ex) {
  120. System.out.print("存储过程参数错误: " + ex.getMessage());
  121. } finally {
  122. if (cstm != null)
  123. cstm.close();
  124. if (conn != null)
  125. conn.close();
  126. }
  127. return cro;
  128. }
  129. private static Connection getNativeConnection(Connection con)
  130. throws SQLException {
  131. if ((con instanceof DelegatingConnection)) {
  132. Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate();
  133. return nativeCon != null ? nativeCon : con.getMetaData().getConnection();
  134. }
  135. return con;
  136. }
  137. }