Search This Blog

Sunday 18 December 2011

Difference between Statement and Prepared Statement

Statement:

When you use normal statements compilation of statement or
parsing of statement will happen every time. This is time
consuming when u have multiple queries to execute.

Statement s = conn.createStatement();
s.executeQuery();

Prepared Statement:
In prepared statement SQL query will be compiled or parsed
for the very first time and kept in some kind of pool. When
u execute one more time the same statement with different
values it saves the parsing time.

The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.

The PreparedStatement may be parametrized.
Prepared statement is precompiled statement it is used when
we want one query to be executed n no. of times.
Prepared Statement is Pre-compiled class , but Statement is not.
So in PreparedStatement the execution will be faster.

SQL select * from employee where employeeID=?,empName=?;
PreparedStatement ps = conn.PreparedStatement();
ps.execute(1,aru);
ps.execute(2,arya);

Callable Statement:

Callable statements are used to execute stored procedures
similar to java functions.

No comments:

Post a Comment