本文介绍了在executeBatch()之前如何检查Prepared语句是否有一些批处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的预处理语句有疑问,因为我对此并不了解.

I have a doubt related to Prepared Statement in Java as I don't know much about it.

我有一个用例,其中必须使用PreparedStatement.但是我在写代码之前只是在想:

I have a use case in which I have to use PreparedStatement. But I am just thinking before writing code that:

while(...)
{
   if(...)
   {
     preparedStatement.setString(1, fname);
     preparedStatement.setString(2, lname); 
      ...
     preparedStatement.addBatch();
   }
}
preparedStatement.executeBatch();

让我们考虑上面的代码.有一个while循环,它将执行一些工作,然后检查一些条件,然后将批处理添加到prepared语句中.

Lets Consider the above code. There is a while loop which will do some stuff and then check some condition and then add the batch in prepared statement.

1)因此,我的问题是,假设没有在准备好的语句中添加任何批处理,而我仍然说executeBatch.它会给我例外还是会执行正常?

1) So, my question is that suppose none of the batch is added in prepared statement and still I say executeBatch. Does it give me exception or will execute fine?

2)有什么方法可以在执行前检查准备状态中的批次?

2) Is there any method to check the batch in preparedstatement before execute?

谢谢.

推荐答案

它应该可以正常工作并返回一个空数组.知道当前是否添加批次的唯一方法是AFAIK,请自己跟踪.

It should just work fine and return an empty array. The only way to know if there are batches currently added, AFAIK, is to keep track of them yourself.

另一方面,如果您只想知道实际执行了多少条语句,那么您可以使用返回的更新计数数组来检查运行了多少查询,以及各个查询值,以查看它们是否成功.请参见 executeBatch() 以获得更多详细信息.

If on the other hand you only want to know how many statements you actually executed, then you can use the array of update counts that was returned to check how many queries were run, and the individual values therein to see for each of them if they were successful. See the documentation of executeBatch() for more details.

这篇关于在executeBatch()之前如何检查Prepared语句是否有一些批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:26