3

When we are creating the Object of the PreparedStatement and execute the Query like that

PreparedStatement stmt=con.prepareStatement("select * from emp");  
ResultSet rs=stmt.executeQuery(); 

how the Statement is recompile and PreparedStatement is precompiled?

when the data is fetched using PreparedStatement object from database at which memory location data is stored?

Damodaran
  • 10,882
  • 10
  • 60
  • 81
R B
  • 55
  • 8
  • Your question is not clear at all. Java has no access to memory locations. Sounds like you need to do some more study. I suggest the Java Tutorial, [Using Prepared Statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) and some googling. – Basil Bourque Nov 04 '13 at 06:54
  • 3
    visit this [link](http://stackoverflow.com/questions/8959036/statement-vs-prepared-statement-in-terms-of-precompilation).RB i think it would be helpful... – salman nizamani Nov 04 '13 at 06:56
  • @BasilBourque when the data is fetched then at some place it is also stored I think. – R B Nov 04 '13 at 07:04

2 Answers2

2

It depends on the JDBC engine. For instance, the JDBC engine for MySQL often doesn't actually create a server-side prepared statement (see MySQL docs and this SO question). In those cases, the PreparedStatement interface only provides a separation between the query and parameters, for clarity and protection from injection attacks; every time you execute the PreparedStatement, it will send over a fully-formed SQL query, which the MySQL server will then parse, optimize and execute. On the other hand, some systems (including MySQL with the right options -- see that second link) will use a "real" prepared statement, which means it'll only get parsed and optimized once.

But really, this is like asking for the memory characteristics of java.util.List -- it's entirely up to the implementation, and thus can't be meaningfully answered for the interface in general.

Community
  • 1
  • 1
yshavit
  • 42,327
  • 7
  • 87
  • 124
1

In JDBC three things always takes place :

  1)Query creation.

  2)Query compilation.

  3)Query Execution.

In case of PreparedStatement, cache memory comes into picture hence first two steps are not needed to follow again. Hence,only last step is executed in case of PreparedStatement which is opposite to Statement.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70