PreparedStatements are JDBC objects that you prepare (compile) once and execute multiple times. See the figure below. If your application executes statements that are almost but not exactly alike, use PreparedStatements, which can contain dynamic or IN parameters. Instead of using the literals for changing parameters, use question marks (?) as placeholders for such parameters. Provide the values when you execute the statement.
Derby supports the ParameterMetaData interface. This interface describes the number, type, and properties of prepared statement parameters. See the Derby Developer's Guide for more information.
A connection need only compile a PreparedStatement once. Subsequent executions can use the same statement execution plan even if the parameter values are different, as shown in the following figure. (PreparedStatements are not shared across connections.)
When, in the same database, an application submits an SQL Statement that exactly matches one already in the cache, Derby grabs the statement from the cache, even if the Statement has already been closed by the application.
To match exactly with a statement already in the cache, the SQL Statement must meet the following requirements:
The following figure shows how Derby can reuse a statement execution plan that is already in the statement cache when the SQL text matches a prior statement exactly, even when the statement is executed from a different connection. PreparedStatements are much more efficient, however.