简体   繁体   中英

PreparedStatement in JDBC

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM