{"record":{"id":"fea62fa043ea0dd5","repo":"apache/shardingsphere","slug":"execute-with-sql-for-preparedstatement","errorCode":null,"errorMessage":"execute with SQL for PreparedStatement","messagePattern":"execute 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":114,"sourceCode":"    \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\n    public final boolean execute(final String sql) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"execute with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"execute with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final boolean execute(final String sql, final int[] columnIndexes) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"execute with SQL for PreparedStatement\");\n    }\n    \n    @Override\n    public final boolean execute(final String sql, final String[] columnNames) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"execute with SQL for PreparedStatement\");\n    }\n}\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java#L96-L132","documentation":"ShardingSphere's PreparedStatement does not support execute(String sql) — the boolean-returning execute that accepts a new SQL string. Because the routed and rewritten SQL is fixed when prepareStatement(sql) is called, executing a different statement through the same object would defeat sharding. All String overloads of execute are final and throw SQLFeatureNotSupportedException.","triggerScenarios":"Calling ps.execute(\"SELECT ...\") or ps.execute(\"INSERT ...\") — any boolean execute(String ...) — on a PreparedStatement from ShardingSphereDataSource. Works on vanilla drivers, throws here.","commonSituations":"Generic DAO layers that decide between query and update at runtime by calling execute(sql) regardless of statement type; dynamic-SQL frameworks; code migrated from Statement where execute(String) was the norm.","solutions":["Call the parameterless ps.execute() and keep the SQL only in prepareStatement(sql).","If the code needs to know whether a ResultSet came back, use the boolean return of execute() and then getResultSet()/getUpdateCount().","Use ps.executeQuery() or ps.executeUpdate() directly when the statement kind is known.","For truly dynamic SQL strings per call, use Statement.execute(sql) instead."],"exampleFix":"// before\nPreparedStatement ps = conn.prepareStatement(sql);\nboolean isRs = ps.execute(sql); // throws\n\n// after\nPreparedStatement ps = conn.prepareStatement(sql);\nps.setInt(1, 42);\nboolean isRs = ps.execute();","handlingStrategy":"try-catch","validationCode":"if (stmt instanceof PreparedStatement && sqlArgument != null) {\n    throw new IllegalArgumentException(\"execute(String) unsupported on PreparedStatement (ShardingSphere)\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    boolean isRs = ps.execute(sql);\n} catch (SQLFeatureNotSupportedException e) {\n    boolean isRs = ps.execute();\n}","preventionTips":["Write separate code paths for Statement and PreparedStatement instead of sharing execute(sql).","Use query()/update() style helper methods that hide the execute-family choice.","Add a unit test asserting your DAO never calls String overloads on prepared statements."],"tags":["jdbc","preparedstatement","execute","unsupported-operation","shardingsphere"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}