{"record":{"id":"9bbff11c8ecb022e","repo":"apache/shardingsphere","slug":"executeupdate-with-sql-for-preparedstatement","errorCode":null,"errorMessage":"executeUpdate with SQL for PreparedStatement","messagePattern":"executeUpdate with SQL for PreparedStatement","errorType":"exception","errorClass":"SQLFeatureNotSupportedException","httpStatus":null,"severity":"error","filePath":"jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java","lineNumber":94,"sourceCode":"    \n    @Override\n    public final void setRowId(final int parameterIndex, final RowId x) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"setRowId\");\n    }\n    \n    @Override\n    public final void setRef(final int parameterIndex, final Ref x) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"setRef\");\n    }\n    \n    @Override\n    public final ResultSet executeQuery(final String sql) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"executeQuery with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final int executeUpdate(final String sql) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"executeUpdate with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"executeUpdate with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"executeUpdate with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final int executeUpdate(final String sql, final String[] columnNames) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"executeUpdate with SQL for PreparedStatement\");\n    }\n    \n    @Override","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java#L76-L112","documentation":"SQLFeatureNotSupportedException('executeUpdate with SQL for PreparedStatement') thrown by AbstractUnsupportedOperationPreparedStatement.executeUpdate(String) (and identically by its int/int[]/String[] overloads). JDBC forbids supplying SQL to a PreparedStatement's execute methods; ShardingSphere makes the overrides final and only implements no-arg executeUpdate() (ShardingSpherePreparedStatement:206) since routing requires the bound values.","triggerScenarios":"ps.executeUpdate(sql) — or executeUpdate(sql, Statement.RETURN_GENERATED_KEYS) — on a ShardingSphere PreparedStatement; typical of generic write helpers and of code that needs generated keys and reaches for the Statement-style overload.","commonSituations":"Central DAO save()/update() methods shared by Statement and PreparedStatement paths; code requesting RETURN_GENERATED_KEYS via executeUpdate(sql, int); Statement examples reused on a PreparedStatement.","solutions":["Use ps.executeUpdate() after setXXX; declare generated-key return in conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) and read ps.getGeneratedKeys().","For raw dynamic SQL, use Statement.executeUpdate(sql[, autoGeneratedKeys]) from conn.createStatement().","Split generic helpers into Statement and PreparedStatement branches so the sql argument is never passed to the latter."],"exampleFix":"// before\nint n = ps.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);\n\n// after\nPreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);\nps.setInt(1, id);\nint n = ps.executeUpdate();\ntry (ResultSet keys = ps.getGeneratedKeys()) { ... }","handlingStrategy":"validation","validationCode":"// Declare generated keys at prepare time; execute with no args\nPreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);\n// then ps.executeUpdate() and ps.getGeneratedKeys()","typeGuard":"static int update(final Statement stmt, final String sql) throws SQLException {\n    return (stmt instanceof PreparedStatement) ? ((PreparedStatement) stmt).executeUpdate() : stmt.executeUpdate(sql);\n}","tryCatchPattern":null,"preventionTips":["Request RETURN_GENERATED_KEYS in prepareStatement(...), not executeUpdate(sql, int)","Use no-arg executeUpdate on PreparedStatement","Split DAO helpers into Statement/PreparedStatement code paths"],"tags":["jdbc","shardingsphere","preparedstatement","api-misuse","generated-keys"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}