• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PreparedStatement!

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


please can anyone explain me the meaning of precompiled ...

thanks & regards,
seetharaman
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the PreparedStatement API: "A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times."

The alternative is a Statement in which data is not known about the query until it is executed. The common example is if you need to do 100 inserts. With a PreparedStatement you can use the same statement 100 times, just changing some of the input values inbetween update calls. With a regular statement, this is compiled at runtime and cannot be changed.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
From the PreparedStatement API: "A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times."

The alternative is a Statement in which data is not known about the query until it is executed. The common example is if you need to do 100 inserts. With a PreparedStatement you can use the same statement 100 times, just changing some of the input values inbetween update calls. With a regular statement, this is compiled at runtime and cannot be changed.



Scott,
Lets have a small pseudo code of what you said
(If we are going to insert 100 records means usually we will use the loop)

String var = "a";
String query="Insert...";

for()
{
........
preparedstatement.setString(1,var);
executeInsert
}

In the above pseudo code instead of prepared statement if I am using statement then also I can change the value at runtime like this correct ?

String var = "a";
String query="Insert..." + var;

for()
{
........
var = ...
executestatement
}


So I can assign values at runtime in both the statements then what Pre compile exactly means ?

I thought that It is for performance improvement.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,
When the driver/database/connection sees a SQL statement, it figures out how to execute it. If the string matches exactly one executed recently, it re-uses the plan. This is related to pre-compiling. In your first example, the SQL is exactly the same since a question mark is used. In your second example, the query is different and the database treats it like a query it is never seen before.

Additionally, you can use batching for insert SQL statements. With prepared statements, you make this even more explicit because you set the SQL once and just set batches of parameters.
 
chandra mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne,

I got your point about precomiplation but I have one more doubt. You said in my second example the query is different.I am just changing the value not the query so how it would be different ?
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Chandra,

YOU know that the query is the same (with just a different value of the parameter), but the database sees a different String than before, therefore thinks it's a new query.

Herman
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,
Herman is correct. This is why I emphasized the word "exactly". The database isn't smart enough to know to things are the same unless they are identical.
 
chandra mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
I got it now.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic