繁体   English   中英

在JDBC中的单个语句对象上使用batchExecute和execute方法

[英]Using batchExecute and execute methods on a single statement object in JDBC

我遇到了一段旧代码,如下所示

Statement stmt = connection.createStatement();
stmt.addBatch(insertQuery);
stmt.addBatch(insertQuery);
stmt.addBatch(insertQuery);
stmt.addBatch(insertQuery);

//there is some data which needs to be deleted before inserting the new data. 
stmt.execute(deleteQuery);
stmt.executeBatch();

在这里,我们正在批处理一些查询,在执行批处理之前,此代码将执行其他一些删除查询,然后执行批处理。

这样做合法吗?

上面的代码是否会按预期工作,它将首先执行删除查询,然后批量更新?

JDBC规范 (版本4.3)说:

statement的批处理为非空时,方法executeQueryexecuteUpdateexecute的行为由实现定义。

换句话说,该行为未指定,并且取决于驱动程序的实现,这意味着不应依赖该行为。

快速(但不彻底)扫描pgjdbc源似乎表明PostgreSQL驱动程序确实允许您首先向批处理中添加语句,执行单个语句,然后执行批处理。

但是在所示的代码中,我建议您先执行删除查询,然后再填充并执行批处理。 对于不熟悉该代码的人来说,该命令更容易阅读。

暂无
暂无

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

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