コード例 #1
0
 /** @throws Exception */
 public void delete() throws Exception {
   Connection delConnection = null;
   String sName = (String) oInput.get("archetype");
   if (!SourceVersion.isName(sName))
     throw new Exception("name " + sName + " is not a valid java identifier");
   PreparedStatement oStmt = null;
   try {
     delConnection = OpenShiftDerbySource.getConnection();
     oStmt = delConnection.prepareStatement("DELETE FROM " + sName + " WHERE id" + sName + " = ?");
     oStmt.setString(1, (String) oInput.get("id" + sName));
     int nRows = oStmt.executeUpdate();
     if (nRows == 0) throw new Exception("no rows deleted");
   } finally {
     if (delConnection != null)
       try {
         delConnection.close();
       } catch (SQLException logOrIgnore) {
       }
     if (oStmt != null)
       try {
         oStmt.close();
       } catch (SQLException logOrIgnore) {
       }
   }
 }
コード例 #2
0
  /** @throws Exception */
  public void save() throws Exception {
    // these are external variables
    Connection insConnection = null;
    PreparedStatement oStmt = null;
    ResultSet rs = null;
    try {
      // get idDispClass
      insConnection = OpenShiftDerbySource.getConnection();
      String sName = (String) oInput.get("archetype");
      oStmt = insConnection.prepareStatement("SELECT idDispClass FROM DispClass WHERE name = ?");
      oStmt.setString(1, sName);
      rs = oStmt.executeQuery();
      rs.next();
      long idDispClass = rs.getLong(1);
      oStmt.close();
      rs.close();
      // get attributes to build sql
      oStmt =
          insConnection.prepareStatement("SELECT name FROM DispAttribute where idDispClass = ?");
      oStmt.setLong(1, idDispClass);
      rs = oStmt.executeQuery();
      String sSQL = null;
      boolean bInsert = false;
      aBindVars.clear();
      if (oInput.get("id" + oInput.get("archetype")) == null) {
        sSQL = doInsert(rs);
        bInsert = true;
      } else {
        sSQL = doUpdate(rs);
      }
      oStmt.close();
      oStmt = insConnection.prepareStatement(sSQL, Statement.RETURN_GENERATED_KEYS);
      int nColumn = 1;

      // set the bindvars, whice were previously cleared and updated by either the insert or update
      // SQL production
      for (String sValue : aBindVars) {
        oStmt.setString(nColumn++, sValue);
      }
      int nRowsAffected = oStmt.executeUpdate();
      if (nRowsAffected == 0) throw new Exception("0 rows updated by insert");
      if (bInsert) {
        // set the inserted id in to our object
        rs.close();
        rs = oStmt.getGeneratedKeys();
        if (rs.next()) {
          set("id" + sName, rs.getString(1));
        } else {
          throw new SQLException("Creating " + sName + " failed, no generated key obtained.");
        }
      }
    } finally {
      if (insConnection != null)
        try {
          insConnection.close();
        } catch (SQLException logOrIgnore) {
        }
      if (rs != null)
        try {
          rs.close();
        } catch (SQLException logOrIgnore) {
        }
      if (oStmt != null)
        try {
          oStmt.close();
        } catch (SQLException logOrIgnore) {
        }
    }
  }