Пример #1
0
  /**
   * Creates a new <code>RunEntity</code> record in the database.
   *
   * <p>The run will be created for the specified unit-of-learning with the specified title.
   *
   * @param aRunDto the id of the unit-of-learning for this run
   * @return the primary key of the new RunEntity, being the runId
   * @throws CreateException if the new record cannot be created in the database
   */
  public RunEntityPK ejbCreate(RunDto aRunDto) throws CreateException {

    setRunDto(aRunDto);
    try {
      Connection connection = Util.getInstance().getConnection();
      try {
        String sql = "INSERT INTO Run (UolID, Name, Starttime) VALUES (?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        try {
          statement.setInt(1, getRunDto().getUolId());
          statement.setString(2, getRunDto().getTitle());
          statement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
          if (statement.executeUpdate() != 1) {
            throw new CreateException("Could not create RunEntity for: " + getRunDto().getUolId());
          }

          int id = Util.getInstance().getIdentity(connection, "run", "RunId");
          if (id != -1) {
            setRunDto(
                new RunDto(id, aRunDto.getUolId(), aRunDto.getTitle(), aRunDto.getStarttime()));
            modified = false;
            return new RunEntityPK(id);
          }
          throw new CreateException("Could not create RunEntity for: " + getRunDto().getUolId());
        } finally {
          statement.close();
        }
      } finally {
        connection.close();
      }
    } catch (SQLException ex) {
      throw new CreateException(ex.getMessage());
    }
  }
Пример #2
0
  public void ejbStore() {
    if (!modified) return;

    RunEntityPK pk = (RunEntityPK) entityContext.getPrimaryKey();
    try {
      Connection connection = Util.getInstance().getConnection();
      try {
        PreparedStatement statement =
            connection.prepareStatement(
                "UPDATE Run SET UolId=?, Name=?, Starttime=? WHERE RunId=?");
        try {
          statement.setInt(1, runDto.getUolId());
          statement.setString(2, runDto.getTitle());
          statement.setTimestamp(3, new Timestamp(runDto.getStarttime().getTimeInMillis()));
          statement.setInt(4, pk.runId);
          if (statement.executeUpdate() != 1) {
            throw new EJBException("Could not update " + pk);
          }
        } finally {
          statement.close();
        }
      } finally {
        connection.close();
      }
    } catch (Exception ex) {
      throw new EJBException(ex);
    }
  }