Friday, March 13, 2009

What is the difference between Statement, PreparedStatement and

Question :What is the difference between Statement, PreparedStatement and
CallableStatemen? (JDBC)

Answer :Statement is used for static SQL statement with no input and output
parameters, PreparedStatement is used for dynamic SQL statement with
input parameters and CallableStatement is used for dynamic SQL satement
with both input and output parameters, but PreparedStatement and
CallableStatement can be used for static SQL statements as well.
CallableStatement is mainly meant for stored procedures.
PreparedStatement gives better performance when compared to Statement
because it is pre-parsed and pre-compiled by the database once for the
first time and then onwards it reuses the parsed and compiled statement.
Because of this feature, it significantly improves performance when a
statement executes repeatedly, It reduces the overload incurred by parsing
and compiling.
CallableStatement gives better performance when compared to
PreparedStatement and Statement when there is a requirement for single
request to process multiple complex statements. It parses and stores the
stored procedures in the database and does all the work at database itself
that in turn improves performance. But we loose java portability and we
have to depend up on database specific stored procedures.

No comments: