{"record":{"id":"bbd056bd3324bdea","repo":"apache/beam","slug":"error-while-setting-data-to-preparedstatement","errorCode":null,"errorMessage":"Error while setting data to preparedStatement","messagePattern":"Error while setting data to preparedStatement","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java","lineNumber":2618,"sourceCode":"                (index) -> {\n                  Schema.FieldType fieldType = fields.get(index).getField().getType();\n                  preparedStatementFieldSetterList.add(\n                      JdbcUtil.getPreparedStatementSetCaller(fieldType));\n                });\n      }\n\n      @Override\n      public void setParameters(T element, PreparedStatement preparedStatement) throws Exception {\n        Row row = (element instanceof Row) ? (Row) element : toRowFn.apply(element);\n        IntStream.range(0, fields.size())\n            .forEach(\n                (index) -> {\n                  try {\n                    preparedStatementFieldSetterList\n                        .get(index)\n                        .set(row, preparedStatement, index, fields.get(index));\n                  } catch (SQLException e) {\n                    throw new RuntimeException(\"Error while setting data to preparedStatement\", e);\n                  }\n                });\n      }\n    }\n\n    private boolean hasStatementAndSetter() {\n      return getStatement() != null && getPreparedStatementSetter() != null;\n    }\n  }\n\n  private static class Reparallelize<T> extends PTransform<PCollection<T>, PCollection<T>> {\n    @Override\n    public PCollection<T> expand(PCollection<T> input) {\n      // See https://issues.apache.org/jira/browse/BEAM-2803\n      // We use a combined approach to \"break fusion\" here:\n      // (see https://cloud.google.com/dataflow/service/dataflow-service-desc#preventing-fusion)\n      // 1) force the data to be materialized by passing it as a side input to an identity fn,\n      // then 2) reshuffle it with a random key. Initial materialization provides some parallelism","sourceCodeStart":2600,"sourceCodeEnd":2636,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java#L2600-L2636","documentation":"During JdbcIO write, each schema field is bound to the PreparedStatement via a registered field setter (preparedStatementFieldSetterList). If ResultSet/row field binding throws SQLException while calling set(...) for any index, it is wrapped in this RuntimeException. It indicates the row data could not be written into the SQL statement parameters.","triggerScenarios":"Writing a Beam Row via JdbcIO.write() where a field value is incompatible with the target column (type mismatch, value out of range for the column type, inserting NULL into a NOT NULL column), or a setter registered for the field throws during preparedStatement.setXxx().","commonSituations":"Schema evolved (e.g. string longer than VARCHAR column), logical types not matching driver expectations, inserting nulls into non-nullable columns, or timezone/precision issues with DATETIME/TIMESTAMP fields.","solutions":["Read the wrapped SQLException cause to identify which parameter/column failed and why.","Ensure the Beam Row schema matches the table column types and nullability.","Truncate or convert values that exceed column size/precision before writing.","Provide a custom statement and PreparedStatementSetter via withStatement()/withPreparedStatementSetter() to control binding yourself."],"exampleFix":"// before\nrow -> Row.withSchema(schema).addValues(longString) // exceeds VARCHAR(10)\n// after\nrow -> Row.withSchema(schema).addValues(longString.substring(0, 10)) // or widen the column","handlingStrategy":"validation","validationCode":"// validate row data against table DDL before writing\nfor (Schema.Field f : row.getSchema().getFields()) {\n  Object v = row.getValue(f.getName());\n  if (v == null && !f.getType().getNullable()) {\n    throw new IllegalArgumentException(\"NOT NULL column has no value: \" + f.getName());\n  }\n  if (v instanceof String && ((String) v).length() > maxLenForColumn(f.getName())) {\n    throw new IllegalArgumentException(\"Value too long for column: \" + f.getName());\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  rows.apply(JdbcIO.<Row>writeWithSchema());\n} catch (RuntimeException e) {\n  if (\"Error while setting data to preparedStatement\".equals(e.getMessage())) {\n    SQLException sql = (SQLException) e.getCause(); // find failing parameter via cause chain\n  }\n  throw e;\n}","preventionTips":["Keep the Beam Row schema in sync with the table DDL (types, lengths, nullability).","Coerce logical types (UUID, enums) to JDBC-friendly types before writing.","Log the offending row on failure to reproduce binding issues offline."],"tags":["jdbc","java","prepared-statement","database-write"],"backgroundTag":"database-write-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}