Lew said:
Lew wrote :
In fact, the DB side could be completely unable to tell the difference,
since the JDBC layer could hide it.
Do a Google search on: sql prepared statement -java -jdbc
The first return was an IBM DB/2 manual
http://publib.boulder.ibm.com/infoc...topic=/com.ibm.db2.udb.doc/admin/r0000975.htm
About 2/3rds way down the page is this statement:
"A prepared statement can be executed many times. Indeed, if a prepared
statement is not executed more than once and does not contain parameter
markers, it is more efficient to use the EXECUTE IMMEDIATE statement
rather than the PREPARE and EXECUTE statements."
Another entry was the PostrgeSQL at address
http://www.postgresql.org/docs/current/static/sql-prepare.html
where it states in the Notes:
"In some situations, the query plan produced for a prepared statement
will be inferior to the query plan that would have been chosen if the
statement had been submitted and executed normally. This is because
when the statement is planned and the planner attempts to determine the
optimal query plan, the actual values of any parameters specified in
the statement are unavailable. PostgreSQL collects statistics on the
distribution of data in the table, and can use constant values in a
statement to make guesses about the likely result of executing the
statement. Since this data is unavailable when planning prepared
statements with parameters, the chosen plan may be suboptimal."
For Microsoft at address
http://support.microsoft.com/kb/224587
it states
"Ideally, an application must prepare a SQL statement one time and run
it several times. This saves the Optimizer the cost of compiling a new
plan each time the statement is executed. Therefore, the number of Exec
Prepared SQL events should be much larger than the number of Prepare
SQL events. If the number of Prepare SQL events is roughly equivalent
to the number of Exec Prepared SQLevents, this may indicate that the
application is not making good use of the prepare/execute model. It is
best not to prepare a statement that is only going to be executed a
single time."
So it seems that the DB engine does more work, rather than JDBC. And
that the PreparedStatement may be less efficient than a Statement.
If the underlying DB does not support PreparedStatement, then of course
the JDBC driver should implement it. Of course it must then also handle
any special characters as per Chris post.