{"record":{"id":"1aa65358825d9303","repo":"pentaho/pentaho-kettle","slug":"unable-to-prepare-statement-for-sql-statement-insertupdate","errorCode":null,"errorMessage":"Unable to prepare statement for SQL statement [","messagePattern":"Unable to prepare statement for SQL statement \\[","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/insertupdate/InsertUpdate.java","lineNumber":392,"sourceCode":"          data.lookupParameterRowMeta.addValueMeta(\n            rowMeta.searchValueMeta( meta.getKeyFields()[ i ].getKeyStream() ).clone() );\n\n        } else {\n          sql += \" \" + meta.getKeyFields()[ i ].getKeyCondition() + \" ? \";\n          data.lookupParameterRowMeta.addValueMeta(\n            rowMeta.searchValueMeta( meta.getKeyFields()[ i ].getKeyStream() ) );\n        }\n      }\n      sql += \" ) ) \";\n    }\n\n    try {\n      if ( log.isDetailed() ) {\n        logDetailed( \"Setting preparedStatement to [\" + sql + \"]\" );\n      }\n      data.prepStatementLookup = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) );\n    } catch ( SQLException ex ) {\n      throw new KettleDatabaseException( \"Unable to prepare statement for SQL statement [\" + sql + \"]\", ex );\n    }\n  }\n\n  // Lookup certain fields in a table\n  public void prepareUpdate( RowMetaInterface rowMeta ) throws KettleDatabaseException {\n    DatabaseMeta databaseMeta = meta.getDatabaseMeta();\n    data.updateParameterRowMeta = new RowMeta();\n\n    String sql = \"UPDATE \" + data.schemaTable + Const.CR;\n    sql += \"SET \";\n\n    boolean comma = false;\n\n    for ( int i = 0; i < meta.getUpdateFields().length; i++ ) {\n      if ( meta.getUpdateFields()[ i ].getUpdate().booleanValue() ) {\n        if ( comma ) {\n          sql += \",   \";\n        } else {","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/insertupdate/InsertUpdate.java#L374-L410","documentation":"setLookup failed to prepare the JDBC PreparedStatement used for the lookup (SELECT ... WHERE key fields) against the target table. SQLExceptions from connection.prepareStatement are wrapped in a KettleDatabaseException including the SQL text.","triggerScenarios":"Invalid SQL generated from step metadata (bad table/column names, wrong quoting), or the JDBC driver/database cannot prepare the statement (missing table, permission denied, unsupported syntax, bad connection).","commonSituations":"Target table doesn't exist yet; reserved word used as a column without quoting; wrong schema name; wrong or outdated JDBC driver; connection pointing to the wrong database.","solutions":["Click SQL on the step to see/execute the generated SQL; create the missing table or fix column names.","Verify Schema/Table names and key column names in the step settings.","Test the database connection (wrong host/db/driver produces prepare failures).","Check the wrapped SQLException cause in the log for the real database error (permissions, syntax, missing object)."],"exampleFix":"// before: table name typo causes prepare failure\nmeta.setTableName(\"custmor\");\n// after\nmeta.setTableName(\"customer\"); // existing table in schema","handlingStrategy":"try-catch","validationCode":"// before running, ensure the lookup table exists\nDatabase db = new Database(transMeta, meta.getDatabaseMeta());\ndb.connect();\nif (!db.checkTableExists(schema, table))\n  throw new IllegalStateException(\"Lookup table missing: \" + table);","typeGuard":"null","tryCatchPattern":"try { trans.execute(...); } catch (KettleDatabaseException e) {\n  if (e.getMessage().startsWith(\"Unable to prepare statement\")) {\n    Throwable sqlEx = e.getCause(); // inspect real SQLException\n  }\n  throw e;\n}","preventionTips":["Run the step dialog's SQL button to create/verify tables before production runs.","Test the database connection with the exact credentials used at runtime.","Avoid reserved words as column names or ensure proper quoting.","Pin and test the correct JDBC driver version."],"tags":["kettle","jdbc","prepared-statement","database"],"backgroundTag":"sql-query-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}