Difference between Statement and PreparedStatement ?




Difference between Statement and PreparedStatement ?

What is Statement ?

  • Statement is a JDBC interface used for general-purpose access to the SQL database, especially while using static SQL statements at the runtime.
  • It defines methods and properties to interact with the database using SQL or PL/SQL commands.
  • It is used to execute standard SQL statements such as CREATE, UPDATE, RETREIVE, and DELETE (CRUD).
  • It works as a carrier between a Java program and the database but it cannot accept parameters at runtime and it’s not pre-compiled meaning the same SQL statement cannot be used multiple times which accounts for slow performance than PreparedStatement.

What is PreparedStatement ?

  • It is an extended and the powerful version of the Statement interface which may or may not be parameterized meaning unlike Statement, it can take input parameters which accounts for faster performance.
  • It also helps to write Object Oriented code with setter methods which improve performance of the applications.
  • And the best part, it is executed via a non-SQL binary communication protocol which is basically a non-textual based format used to communicate between clients and servers which ultimately reduces the bandwidth usage thereby promoting faster communication calls to server.

Statement vs prepared statement

Statement PreparedStatement
It is used to execute normal SQL queries. It is used to execute parameterized or dynamic
SQL queries.
It is preferred when a particular SQL query is to be
executed only once.
It is preferred when a particular query is to be
executed multiple times.
You cannot pass the parameters to SQL query using this
interface.
You can pass the parameters to SQL query at run
time using this interface.
This interface is mainly used for DDL statements like
CREATE, ALTER, DROP etc.
It is used for any kind of SQL queries which are to
be executed multiple times.
The performance of this interface is very low. The performance of this interface is better than the Statement interface (when used for multiple execution of same query).

Related Searches to Difference between Statement and PreparedStatement ?