| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package UIB.JHY;
- import CoreFS.SA01.CoreIComponent;
- import CoreFS.SA06.CoreReturnObject;
- import CoreFS.SA06.CoreSqlType.CoreOracleType;
- import UIB.COM.XmlSqlParsersFactory;
- import oracle.sql.ARRAY;
- import oracle.sql.ArrayDescriptor;
- import org.apache.commons.dbcp.DelegatingConnection;
- import java.sql.Array;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.*;
- public class ComCallProc extends CoreIComponent {
- /**
- * @param sqlID xml中的SQL的ID
- * @param ht 前台需要用hashtable传入,所有参数均存入hashmap中 ,前台必须以(I1,""),(I2,""),,(O1,""),,(O2,"")的方式存入
- * I表示in,O表示out。
- * @return CoreReturnObject
- * @throws Exception
- * @desc 执行存储过程
- */
- public CoreReturnObject doCoreProc(String dao, String sqlID, HashMap ht) throws Exception {
- CoreReturnObject cro = new CoreReturnObject();
- ArrayList inParam = new ArrayList();//输入参数
- ArrayList inParamType = new ArrayList();//输入参数类型
- ArrayList aList = new ArrayList();
- String key = "";
- ArrayList outParam = new ArrayList();//输出参数
- ArrayList outparamtype = new ArrayList();//输出参数类型
- String[] str = new String[3];
- int index = 0;
- //取出hashmap中的key,存储进ArrayList中
- for (Iterator it = ht.keySet().iterator(); it.hasNext(); ) {
- key = it.next().toString();
- aList.add(key);
- }
- Collections.sort(aList);
- String key_1 = null;
- for (int i = 0; i < aList.size(); i++) {
- key_1 = aList.get(i).toString().toLowerCase();
- if (key_1.startsWith("i"))//输入参数
- {
- if (key_1.indexOf(":") != -1) {
- ArrayList al = null;
- Object obj = ht.get(aList.get(i).toString());//是否按ArrayList传递
- if (obj instanceof ArrayList) {
- al = (ArrayList) obj;
- } else //进行转换
- {
- al = new ArrayList();
- al.add(obj.toString());
- }
- Array ar = this.getDao(dao).getArray(key_1.substring(key_1.indexOf(":") + 1).toUpperCase(), al);
- inParam.add(ar);
- inParamType.add(CoreOracleType.ARRAY_TYPE.ordinal());
- } else//输出参数
- {
- inParam.add(ht.get(aList.get(i).toString()));
- inParamType.add(new Integer(CoreOracleType.STRING_TYPE.ordinal()));
- }
- } else if (key_1.startsWith("o")) {
- //outParam.add(str[index++]);
- // outparamtype.add(new Integer(CoreSqlType.CoreOracleType.STRING_TYPE.ordinal()));
- outparamtype.add(CoreOracleType.STRING_TYPE.ordinal());
- }
- }
- ht = null;
- String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
- cro = this.getDao(dao).ExcuteProcedure(sqlString, inParamType, inParam, outparamtype, outParam);
- // cro = this.getDao(dao).ExcuteProcedure(sqlString, inParamType , inParam );
- return cro;
- }
- /**
- * @param sqlID
- * @param ht
- * @return
- * @throws Exception
- */
- public CoreReturnObject doSimpleProc(String dao, String sqlID, HashMap ht) throws Exception {
- CoreReturnObject cro = new CoreReturnObject();
- if (ht == null) return cro;
- String sqlString = XmlSqlParsersFactory.getSql(sqlID).trim();
- Connection conn = this.getDao(dao).getConnection();
- CallableStatement cstm = conn.prepareCall(sqlString);
- String key = "";
- // BasicDataSource ds = (BasicDataSource)this.dbproxy.getJdbcTemplate().getDataSource();
- try {
- String[] outIndex = new String[3];//最多为3个输出
- int j = 0;
- for (Iterator it = ht.keySet().iterator(); it.hasNext(); ) {
- key = it.next().toString();
- if (key.toLowerCase().startsWith("i")) {
- if (key.toLowerCase().indexOf(":") > 0)//":"不会在字符串的最前面,判断是否包含“:”
- {
- String arrayType = key.substring(key.indexOf(":") + 1).toUpperCase();//数组类型
- ArrayDescriptor ad = ArrayDescriptor.createDescriptor(arrayType, getNativeConnection(conn));
- ARRAY array = new ARRAY(ad, getNativeConnection(conn), ((List) ht.get(key)).toArray());
- cstm.setArray(Integer.parseInt(key.substring(1, 2)), array);
- } else {
- cstm.setString(Integer.parseInt(key.substring(1)), ht.get(key).toString());
- }
- } else if (key.toLowerCase().startsWith("o")) {
- cstm.registerOutParameter(Integer.parseInt(key.substring(1)), java.sql.Types.VARCHAR);
- outIndex[j++] = key.substring(1);
- }
- }
- ht = null;
- if (cstm == null) throw new Exception("获取存储过程出错,请检查!");
- cstm.execute();
- ArrayList aList = new ArrayList();
- //输出
- for (int i = 0; i < outIndex.length; i++) {
- if (outIndex[i] != null) {
- aList.add(cstm.getString(Integer.parseInt(outIndex[i])));
- }
- }
- cro.setResult(aList);
- } catch (Exception ex) {
- System.out.print("存储过程参数错误: " + ex.getMessage());
- } finally {
- if (cstm != null)
- cstm.close();
- if (conn != null)
- conn.close();
- }
- return cro;
- }
- private static Connection getNativeConnection(Connection con)
- throws SQLException {
- if ((con instanceof DelegatingConnection)) {
- Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate();
- return nativeCon != null ? nativeCon : con.getMetaData().getConnection();
- }
- return con;
- }
- }
|