• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared statements question

 
Tristan Van Poucke
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been thinking about prepared statements, and I have a question
In following example(psuedo, well sortoff) I have a method executing a query.

In that method i prepare a statement:



If I call this method several times, will the statement be prepared over and over again?
Thus leaving me only with the security side of the benefits of prepared statements.
 
Kapil Mishra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prepared Statement has a big deal on performance..As database query string which only compiles at database engine one time rather than Statement query which got compiles every time you execute query....

More on this-
PreparedStatement
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think so. If you execute the query. You will only run the command:

Not the statement:

So the query is prepared only once and executed anytime!
For benefits of PreparedStatement, read this:
http://faq.javaranch.com/java/PreparedStatement
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tristan Van Poucke wrote:...
If I call this method several times, will the statement be prepared over and over again?
Thus leaving me only with the security side of the benefits of prepared statements.
It is prepared over and over, because you call it over and over. But you don't only get the security benefit.
Because you use a prepared statement with parameter binding, your database will most likely recognize this query, and reuse it's execution plan. It won't do that if you paste your values into the sql string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic