调用存储过程

 @Override
 public ArrayList<Object> PcdAdd(ArrayList<Object> inParam) throws SQLException {
  //测试用户
//  String billUserName="VOICECAPTCHA";
  String billUserName="PCD";
  ArrayList<Object> resultList=null;
  String procName="SP_PCD_ADD";
  int inputOutCount=inParam.size()+1;
  String callUrl="{call "+billUserName+"."+procName+"(";
  for(int i=0;i<inputOutCount;i++)
  {
   callUrl=callUrl+"?,";
  }
  callUrl=callUrl.substring(0, callUrl.length()-1);
  callUrl=callUrl+")}";
  try {
   jdbcConn = getJdbcConnection();  
   jdbcConn.setAutoCommit(true);    
   CallableStatement proc = jdbcConn.prepareCall(callUrl);
   for(int i=0;i<inParam.size();i++){
    Object obj=inParam.get(i);
    if(obj instanceof String){
     proc.setString(i+1, (String)obj);
    }
     if(obj instanceof Date){
     proc.setDate(i+1, new java.sql.Date(((Date) obj).getTime()));
    }
    if(obj instanceof Integer){
     proc.setInt(i+1, (Integer)obj);
    }
    if(obj instanceof Long){
     proc.setLong(i+1, (Long)obj);
    }
   }
   proc.registerOutParameter(inParam.size()+1, Types.INTEGER);
   proc.execute();
   resultList=new ArrayList<Object>();
   resultList.add(proc.getInt(inParam.size()+1));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally
  {
   try
   {
    if(jdbcConn!=null)
    {
     closeJdbcConnection(jdbcConn);
    }
   }
   catch(Exception ex)
   {
    ex.printStackTrace();
   }
  }
  return resultList;
 }

 

/**
  * 从session工厂中获取JDBC连接
  * @return
  * @throws Exception
  */
 protected Connection getJdbcConnection() throws Exception
 {
  try 
  {
   cp = ((SessionFactoryImplementor)this.sessionFactory).getConnectionProvider();
   jdbcConn = cp.getConnection();
  } 
  catch (Exception e) 
  {
   throw e;
  }
  return jdbcConn;
 }

 

 

/**
  * 将JDBC连接释放到session工厂中
  * @param conn
  * @throws Exception
  */
 protected void closeJdbcConnection(Connection conn) throws Exception
 {
  try
  {
   if(null!=conn)
   {
    if(null==cp)
    {
     cp = ((SessionFactoryImplementor)this.sessionFactory).getConnectionProvider();
    }
    cp.closeConnection(conn);
   }
  } 
  catch (Exception e) 
  {
   throw e;
  }
 }

 

你可能感兴趣的:(存储过程)