简体   繁体   English

在批处理中使用JDBC preparedStatement

[英]using JDBC preparedStatement in a batch

Im using Statement s batchs to query my data base. 我使用Statement的批处理来查询我的数据库。 Iv'e done some research now and i want to rewrite my application to use preparedStatement instead but i'm having hard time to figure out how to add queries to a preparedStatement batch. Iv'e现在做了一些研究,我想重写我的应用程序使用preparedStatement代替,但我有很难搞清楚如何查询添加到preparedStatement批。

This is what i'm doing now: 这就是我现在正在做的事情:

private void addToBatch(String sql) throws SQLException{
sttmnt.addBatch(sql);
batchSize++;
if (batchSize == elementsPerExecute){
    executeBatches();
}
}

where sttmnt is a class member of type Statement . 其中sttmntStatement类型的类成员。

What i want to do is to use the preparedStatement 's setString(int, String) method to set some dynamic data and then add it to the batch. 我想要做的是使用preparedStatementsetString(int, String)方法设置一些动态数据,然后将其添加到批处理中。

Unfortunately, i don't fully understand how it works, and how i can use setString(int, String) to a specific sql in the batch OR create a new preparedStatemnt for every sql i have and then join them all to one batch. 不幸的是,我不完全理解它是如何工作的,以及如何将setString(int, String)用于批处理中的特定sql,或者为我拥有的每个sql创建一个新的preparedStatemnt ,然后将它们全部连接到一个批处理。

is it possible to do that? 有可能这样做吗? or am i really missing something in my understanding of preparedStatement ? 或者我是否真的错过了对preparedStatement理解?

Read the section 6.1.2 of this document for examples. 有关示例,请阅读本文档的6.1.2节 Basically you use the same statement object and invoke the batch method after all the placeholders are set. 基本上,您使用相同的语句对象,并在设置所有占位符后调用批处理方法。 Another IBM DB2 example which should work for any JDBC implementation. 另一个适用于任何JDBC实现的IBM DB2示例 From the second site: 从第二个网站:

try {
  connection con.setAutoCommit(false);        
  PreparedStatement prepStmt = con.prepareStatement(    
    "UPDATE DEPT SET MGRNO=? WHERE DEPTNO=?");
  prepStmt.setString(1,mgrnum1);            
  prepStmt.setString(2,deptnum1);
  prepStmt.addBatch();                      

  prepStmt.setString(1,mgrnum2);                        
  prepStmt.setString(2,deptnum2);
  prepStmt.addBatch();
  int [] numUpdates=prepStmt.executeBatch();
  for (int i=0; i < numUpdates.length; i++) {
    if (numUpdates[i] == -2)
      System.out.println("Execution " + i + 
        ": unknown number of rows updated");
    else
      System.out.println("Execution " + i + 
        "successful: " + numUpdates[i] + " rows updated");
  }
  con.commit();
} catch(BatchUpdateException b) {
  // process BatchUpdateException
} 

With PreparedStatement 's you have wild cards in a way, for example 例如,使用PreparedStatement ,您可以使用通配符

Sring query = "INSERT INTO users (id, user_name, password) VALUES(?,?,?)";
PreparedStatement statement = connection.preparedStatement(query);
for(User user: userList){
    statement.setString(1, user.getId()); //1 is the first ? (1 based counting)
    statement.setString(2, user.getUserName());
    statement.setString(3, user.getPassword()); 
    statement.addBatch();
}

This will create 1 PreparedStatement with that query shown above.You can loop through list when you want to insert or whatever you intentions are. 这将使用上面显示的查询创建1个PreparedStatement 。当您要插入或任何您想要的内容时,您可以遍历列表。 When you want to execute you, 如果你想执行你,

statement.executeBatch();
statement.clearBatch(); //If you want to add more, 
//(so you don't do the same thing twice)

I'm adding an extra answer here specifically for MySQL. 我在这里专门为MySQL添加了一个额外的答案。

I found that the time to do a batch of inserts was similar to the length of time to do individual inserts, even with the single transaction around the batch. 我发现进行一批插入的时间类似于单个插入的时间长度,即使是批处理周围的单个事务也是如此。

I added the parameter rewriteBatchedStatements=true to my jdbc url, and saw a dramatic improvement - in my case, a batch of 200 inserts went from 125 msec. 我将参数rewriteBatchedStatements=true添加到我的jdbc网址,并看到了一个显着的改进 - 在我的情况下,一批200个插入从125毫秒。 without the parameter to about 10 to 15 msec. 没有参数约10至15毫秒。 with the parameter. 用参数。

See MySQL and JDBC with rewriteBatchedStatements=true 请参阅使用rewriteBatchedStatements = true的MySQL和JDBC

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

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