简体   繁体   English

在循环内创建预准备语句

[英]Creation of a prepared statement inside a loop

For clarification: I know that it's the right thing to create the PreparedStatement outside the loop. 澄清一下我知道在循环外创建PreparedStatement是正确的。 I've asked this question just out of curiosity. 出于好奇,我问过这个问题。


Let's assume that I'm creating a PreparedStatement inside a loop with always the same SQL query. 让我们假设我在一个循环中创建一个PreparedStatement并且始终使用相同的SQL查询。

final String sql = "INSERT INTO ...";
while (condition) {
   ...
   PreparedStatement statement = connection.prepareStatement(sql);
   // Fill values of the prepared statement
   // Execute statement
   ...
}

Is this useless since the PreparedStatement object is always created anew? 这是无用的,因为PreparedStatement对象总是重新创建? Or does the underlying database recognise that it's always the same SQL query with which the PreparedStatement is created and reuses it? 或者底层数据库是否认识到它始终与创建PreparedStatement SQL查询相同并重用它?

1. If you are using the same PreparedStatement throughout the loop, then its better you keep the PreparedStatement outside the loop . 1.如果您在整个循环中使用相同的 PreparedStatement ,那么最好将 PreparedStatement 保持 在循环之外

2. If you have sql statment which keeps changing inside the loop , then only its worth using it in the loop. 2.如果你的sql语句在循环中不断变化 ,那么只有它在循环中使用它的价值。

3. Moreover if its keep changing, then just use Statement instead of PreparedStatement , else the very purpose of PreparedStatement is lost as you keep changing it. 3.此外,如果它不断变化,那么只需使用Statement 而不是 PreparedStatement ,否则PreparedStatement的目的就会丢失,因为你不断改变它。

Some drivers do cache prepared statements, yes. 有些驱动程序会缓存预准备语句,是的。 For example, skim this Oracle documentation: http://docs.oracle.com/cd/B10501_01/java.920/a96654/stmtcach.htm 例如,浏览此Oracle文档: http//docs.oracle.com/cd/B10501_01/java.920/a96654/stmtcach.htm

I don't believe there's anything that requires this to be true for all drivers, although certainly it seems like a likely feature of many JDBC drivers. 我不相信有任何事情要求所有驱动程序都是如此,尽管它似乎是许多JDBC驱动程序的一个可能特征。 It sounds like MySQL might not do this: How to use MySQL prepared statement caching? 听起来MySQL可能不会这样做: 如何使用MySQL预处理语句缓存?

That said, if you really want to use prepared statements efficiently, it seems like hanging on to an instance of a prepared statement that you use on each loop iteration makes a lot more sense. 也就是说,如果你真的想要有效地使用预处理语句,那么在每个循环迭代中使用的预处理语句的实例似乎更加有意义。

Two ways so far i know. 到目前为止,我知道两种方式。

1st Way 第一道路

Its insert record one by one 它的插入记录一个接一个

final String sql = "INSERT INTO tablename(columnname) Values(?)";

PreparedStatement statement = connection.prepareStatement(sql);

while (condition) {
statement.setString(1,value);
statement.executeUpdate();
}

(or) (要么)

2nd way 第二种方式

It inserts all record as bulk insert 它将所有记录作为批量插入插入

final String sql = "INSERT INTO tablename(columnname) Values(?)";

PreparedStatement statement = connection.prepareStatement(sql);

while (condition) {
statement.setString(1,value);
statement.addBatch();
}

statement.executeBatch();

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

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