简体   繁体   English

JDBC中的PreparedStatement

[英]PreparedStatement in JDBC

When we are creating the Object of the PreparedStatement and execute the Query like that 当我们创建PreparedStatement的对象并像这样执行查询时

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

how the Statement is recompile and PreparedStatement is precompiled? 如何重新编译该语句并预编译PreparedStatement?

when the data is fetched using PreparedStatement object from database at which memory location data is stored? 当使用PreparedStatement对象从存储内存位置数据的数据库中获取数据时?

It depends on the JDBC engine. 它取决于JDBC引擎。 For instance, the JDBC engine for MySQL often doesn't actually create a server-side prepared statement (see MySQL docs and this SO question ). 例如,用于MySQL的JDBC引擎通常实际上并不创建服务器端准备好的语句(请参阅MySQL文档此SO问题 )。 In those cases, the PreparedStatement interface only provides a separation between the query and parameters, for clarity and protection from injection attacks; 在这些情况下, PreparedStatement接口仅在查询和参数之间提供分隔,以保持清晰度并防止注入攻击。 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. 每次执行PreparedStatement ,它将通过一个完整的SQL查询发送,然后MySQL服务器将对其进行解析,优化和执行。 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. 另一方面,某些系统(包括带有正确选项的MySQL-看到第二个链接)将使用“真实的”准备好的语句,这意味着它将仅被解析和优化一次。

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. 但是,实际上,这就像要求java.util.List的内存特征一样-这完全取决于实现,因此通常无法对接口进行有意义的回答。

In JDBC three things always takes place : 在JDBC中,总是发生三件事:

  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. 在PreparedStatement的情况下,高速缓存会显示在图片中,因此无需再次执行前两个步骤。 Hence,only last step is executed in case of PreparedStatement which is opposite to Statement. 因此,在PreparedStatement与Statement相反的情况下,仅执行最后一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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