Re: Difference between Statement and preparedStatement (for SQL databases) ?

From: jlp (jean-louis.pasturel_at_wanadoo.fr)
Date: 10/18/04


Date: Mon, 18 Oct 2004 18:13:27 +0200

steph wrote:
> Le 16/10/2004 13:07, Ken Philips a écrit :
>
>> Sometimes I can see pure Statement declaration for accessing SQL
>> databases
>> others use preparedStatements.
>>
>> What is the difference?
>
>
> A statement use a String query which is interpreted by the RDBMS.
>
> PreparedStatements are "compiled" on the RDBMS and take parameters
> represented by '?' characters.
>
> More, statement query are build by concating Strings:
> "select a,b from table where a='"+paramA+"' orderby b"
> being very carefull with quote.
>
> PreparedStatement are build like functions:
>
> ps = new ps("insert into table (a,b) values (?,?)");
> loop:
> ps .reset();
> ps.setParam(1,paramA);
> ps.setParam(2,paramB);
> end.
>
> Use a statement if you make the request only once e.g: a simple select.
>
> Use a prepared statement if you use the same request many times e.g: in
> a loop of insert.
>
> In general, prefer a PS for flexibility and performances.
>
>>
>> Ken
>>
>
For example with Oracle, it's preferable to use PreparedStatement to
have binded requests ( compiled in the Oracle SGA). It's better for
Database performance.