• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement doubt

 
Angeline Kanimalar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I understand the PreparedStatement is pre-compiled.

But can anyone tell me,
When it compiles -> compilation time or runtime?
Where it compiles -> DBMS or JVM?

 
Ifteqar Ahmed
Ranch Hand
Posts: 85
Java Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

PreparedStatement gets compiled during java compilation only, but it executes the query during runtime with values provided during runtime of java application and it executes in JVM in all times using JDBC Driver.


hope you got my point......
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, RDBMS can 'compile' SQL statements to build an execution plan in order to make the execution more efficient (using indexes etc)
Not all databases do this, and not all PreparedStatement implementations tap into the database support for statement compilation.

I'm not 100% sure but I believe it commonly works by matching the SQL String with the existing compiled statement, so it should not be necessary to explicitly reuse the same PreparedStatement.

eg http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html
 
Angeline Kanimalar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, now I understand that RDBMS compiles the PreparedStatement.
But when its get compiled? during java compilation or when the first query gets executed?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it gets done by the database the first time the SQL is executed.
The database maintains a pool of these precompiled execution plans and decides when (or if) to forget some to free the memory.
To be honest I doubt you'd notice the difference between a statement that runs from a precompiled execution plan and one that compiles the statement every time, but the database can tell.
 
Angeline Kanimalar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot David. I am clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic