Where is the bug in this code?
<p>In the following code when if condition fails i.e when else block is reached the procedure runs successfully else throws Closed Statement Exception:</p>

<pre><code> public synchronized void create(Map formMap) throws SQLException, Exception
  { 
    CallableStatement callStmt=null;    
    try
    {    
      String sql= "BEGIN UP_I_51(?, ?, ?, ?, ?, ?, ?, ?, ?); END;";   
      callStmt=getConnection().prepareCall(sql);
      callStmt.setString(1,formMap.get("periodFrom").toString());   
      callStmt.setString(2, formMap.get("periodTo").toString());
      String srcType=formMap.get("srcType").toString();
      callStmt.setString(3, srcType);
      callStmt.setString(4, formMap.get("collabIdMod").toString());
      callStmt.setString(5, formMap.get("name").toString());
      callStmt.setString(6, formMap.get("address").toString());
      callStmt.setString(7, " ");
      String parCollabId=formMap.get("pCollabId").toString();
      if(StringUtils.exists(parCollabId))
      {
        callStmt.setString(8, parCollabId);
        String parSrcType=getParentSrcType(srcType);
        callStmt.setString(9, parSrcType);
      }
      else
      {
        callStmt.setString(8, " ");
        callStmt.setString(9, " ");
      }   
      callStmt.executeUpdate(); 
    }
    finally
    {     
      callStmt.close();       
    }
  }
</code></pre>

<p>I replaced the code with below code and it is working now. I have used conditions when setting strings before but don't know why exception comes now.</p>

<p>I still don't understand why this exception comes. Could anyone find it?</p>

<pre><code> public synchronized void create(Map formMap) throws SQLException, Exception
  { 
        CallableStatement callStmt=null;    
        String srcType=formMap.get("srcType").toString();

        String parCollabId=formMap.get("pCollabId").toString();
        String parSrcType=getParentSrcType(srcType);
        try
        {    
            srcType=formMap.get("srcType").toString();
            parCollabId=formMap.get("pCollabId").toString();
            parSrcType=getParentSrcType(srcType);

            parCollabId = StringUtils.exists(parCollabId) ? parCollabId : " ";
            parSrcType = StringUtils.exists(parCollabId) ? parSrcType : " ";

            String sql= "{ call UP_I_51(?, ?, ?, ?, ?, ?, ?, ?, ?) }";  

          callStmt=getConnection().prepareCall(sql);
          callStmt.setString(1,formMap.get("periodFrom").toString());   
          callStmt.setString(2, formMap.get("periodTo").toString());

          callStmt.setString(3, srcType);
          callStmt.setString(4, formMap.get("collabIdMod").toString());
          callStmt.setString(5, formMap.get("name").toString());
          callStmt.setString(6, formMap.get("address").toString());
          callStmt.setString(7, " ");

          callStmt.setString(8, parCollabId);
          callStmt.setString(9, parSrcType);
          callStmt.executeUpdate(); 
        }catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {     
          callStmt.close();       
        }
  }
</code></pre>
