Beispiel #1
0
  @Override
  public void batchWrite(BatchUpdateHandler handler, List<String> batchedSQL) throws SQLException {
    Statement statement = null;
    try {
      statement = connection.createStatement();
      for (String row : batchedSQL) {
        addBatch(statement, row);
      }

      int[] batchResult = executeBatch(statement);
      handler.onBatchResult(Arrays.copyOf(batchResult, batchResult.length));

      ResultSet generatedKeys = getGeneratedKeys(statement);
      if (generatedKeys != null) {
        while (generatedKeys.next()) {
          handler.onGeneratedKey(generatedKeys.getObject(1));
        }
        generatedKeys.close();
      }

      SQLWarning warning = getWarnings(statement);
      if (warning != null) {
        handler.onWarning(warning);
      }
    } finally {
      if (statement != null) {
        try {
          close(statement);
        } catch (SQLException e) {
          LOGGER.error("Unable to close statement after batch write", e);
        }
      }
    }
  }
Beispiel #2
0
  @Override
  public void batchWrite(BatchUpdateHandler handler, String SQL, List<Object[]> parameters)
      throws SQLException {
    PreparedStatement statement = null;
    try {
      statement = prepareBatchUpdateStatement(SQL);

      for (Object[] row : parameters) {
        for (int i = 0; i < row.length; i++) {
          setParameter(statement, row[i], i + 1);
        }
        addBatch(statement);
      }

      int[] batchResult = executeBatch(statement);
      handler.onBatchResult(batchResult);

      ResultSet generatedKeys = getGeneratedKeys(statement);
      if (generatedKeys != null) {
        while (generatedKeys.next()) {
          handler.onGeneratedKey(generatedKeys.getObject(1));
        }
        generatedKeys.close();
      }

      SQLWarning warning = getWarnings(statement);
      if (warning != null) {
        handler.onWarning(warning);
      }
    } finally {
      if (statement != null) {
        try {
          close(statement);
        } catch (SQLException e) {
          LOGGER.error("Unable to close statement after batch write", e);
        }
      }
    }
  }