首页 > 解决方案 > 批处理中的 PreparedStatement clearParameters()

问题描述

我有以下代码片段 -

String sql = "insert into A (c1, c2, c3) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);

for (Employee employee: employees) {

    ps.setString(1, employee.getName());
    ps.setString(2, employee.getCity());

   if (StringUtils.isNotBlank(employee.getPhone()))
   {
       ps.setString(3, employee.getPhone());
   }
   else
   {
       // add a warning log - I do not want to throw an exception 
       ps.clearParameters();
       continue;
   }
    ps.addBatch();
}

ps.executeBatch(); 

上述方法是否有效并且不会将该特定员工记录添加到批次中?我试图重现这一点,但不确定结果,因为我无法访问某些系统。我通读了 clearParameters() 的描述,它确实指出了批处理场景中发生的情况。它是否仅删除该特定记录的参数?如果它取决于底层驱动程序,我使用 H2 数据库。

谢谢!

标签: javajdbch2

解决方案


推荐阅读