975bbc90a9c380a23fb68340797e2678ffb8e325.svn-base 5.4 KB

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