{"record":{"id":"b69fbac4d42f4a8e","repo":"apache/shardingsphere","slug":"executequery-with-sql-for-preparedstatement","errorCode":null,"errorMessage":"executeQuery with SQL for PreparedStatement","messagePattern":"executeQuery 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":89,"sourceCode":"    \n    @Override\n    public final void setNCharacterStream(final int parameterIndex, final Reader x, final long length) throws SQLException {\n        throw new SQLFeatureNotSupportedException(\"setNCharacterStream\");\n    }\n    \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","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationPreparedStatement.java#L71-L107","documentation":"SQLFeatureNotSupportedException('executeQuery with SQL for PreparedStatement') thrown by AbstractUnsupportedOperationPreparedStatement.executeQuery(String). Per the JDBC contract, a PreparedStatement executes its precompiled SQL and executeQuery(String) must throw; ShardingSphere enforces this with a final override. Only the no-arg executeQuery() is implemented (ShardingSpherePreparedStatement:167), because routing depends on the bound parameters.","triggerScenarios":"ps.executeQuery(sql) on a ShardingSphere PreparedStatement; produced by copy-pasted Statement code, or by generic query helpers that take (statement, sql) and never learned the statement's concrete type.","commonSituations":"DAO base classes centralizing execute calls for both Statement and PreparedStatement; refactoring Statement code to PreparedStatement without dropping the sql argument; tutorial-derived code.","solutions":["Use ps.executeQuery() with parameters set via setXXX.","For dynamic SQL strings, create a Statement: conn.createStatement().executeQuery(sql).","Fix generic helpers to dispatch on instanceof PreparedStatement and use the no-arg execute methods."],"exampleFix":"// before\ntry (PreparedStatement ps = conn.prepareStatement(sql)) {\n    ResultSet rs = ps.executeQuery(sql);\n}\n\n// after\ntry (PreparedStatement ps = conn.prepareStatement(sql)) {\n    ps.setInt(1, id);\n    ResultSet rs = ps.executeQuery();\n}","handlingStrategy":"validation","validationCode":"// Validate: PreparedStatement executes only its own precompiled SQL\nif (stmt instanceof PreparedStatement) { rs = ((PreparedStatement) stmt).executeQuery(); } else { rs = stmt.executeQuery(sql); }","typeGuard":"static ResultSet query(final Statement stmt, final String sql) throws SQLException {\n    return (stmt instanceof PreparedStatement) ? ((PreparedStatement) stmt).executeQuery() : stmt.executeQuery(sql);\n}","tryCatchPattern":null,"preventionTips":["Never pass SQL to PreparedStatement execute methods","Use conn.createStatement() for raw SQL queries","Type-check generic query helpers with instanceof PreparedStatement"],"tags":["jdbc","shardingsphere","preparedstatement","api-misuse","sql-feature-not-supported"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}