Ejemplo n.º 1
0
  public License save(License license) throws SQLException {
    try (Connection conn = ds.getConnection()) {
      PreparedStatement statement =
          conn.prepareStatement(
              "INSERT INTO License (productId, releaseId, customerId, contractNumber, state, predecessorLicenseId, "
                  + "validFrom, validTill, applicationSubmitDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
      statement.setInt(1, license.getProduct().getId());
      if (license.getRelease() == null) {
        statement.setNull(2, java.sql.Types.INTEGER);
      } else {
        statement.setInt(2, license.getRelease().getId());
      }
      statement.setInt(3, license.getCustomer().getId());
      statement.setString(4, license.getContractNumber());
      statement.setInt(5, license.getState().getStateNumber());
      statement.setString(6, license.getPredecessorLicenseId());
      statement.setDate(7, license.getValidFrom());
      statement.setDate(8, license.getValidTill());
      // statement.setInt(9, license.getType().getId());
      statement.setDate(9, license.getApplicationSubmitDate());
      statement.execute();

      try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
        if (generatedKeys.next()) {
          license.setId(generatedKeys.getInt(1));
        }
      }
    }
    return license;
  }
Ejemplo n.º 2
0
  public License updateLicense(License license) throws SQLException {
    try (Connection conn = ds.getConnection()) {
      PreparedStatement statement =
          conn.prepareStatement(
              "UPDATE License SET "
                  + "releaseId = ?, state = ?, licenseTypeId = ?, latestDeliveryDate = ? WHERE id = ?;");
      State licenseState = license.getState();
      statement.setObject(1, license.getRelease() == null ? null : license.getRelease().getId());
      statement.setInt(2, licenseState.getStateNumber());
      statement.setInt(3, license.getType().getId());
      statement.setObject(
          4, license.getLatestDeliveryDate() == null ? null : license.getLatestDeliveryDate());
      statement.setInt(5, license.getId());

      int rowCount = statement.executeUpdate();
      if (rowCount == 0) {
        throw new RuntimeException("No license with id: " + license.getId());
      }
    }
    return license;
  }