title: Best way of using PreparedStatement executeBatch()
body: <p>I am trying to figure out the best way of using PreparedStatement executeBatch() method. </p>
  
  <p>One way i tried is:</p>
  
  <pre><code>try{
      prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
      while (operatorsQuery.next() ) {
            logger.info("phone: "+ phone + ",  operator: " + operator);
  
            process(); //0.5-1 second long
            prepStmt1.setString(1, "0"+phone);
            prepStmt1.addBatch();
      }
  prepStmt1.executeBatch();
  }
  catch{...}
  finally{
      closeStatmentand(prepStmt1);
  }
  </code></pre>
  
  <p>The problem i am having with this code is that the program can exit in the middle and then it might not reach the executeBatch() method.</p>
  
  <p>The second way i tried:</p>
  
  <pre><code>try{
      prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
      while (operatorsQuery.next() ) {
            logger.info("phone: "+ phone + ",  operator: " + operator);
  
            process(); //0.5-1 second long
            prepStmt1.setString(1, "0"+phone);
            prepStmt1.addBatch();
            if ((j + 1) % 100 == 0) {
                 prepStmt1.executeBatch();
            }
      }
  prepStmt1.executeBatch();
  }
  catch{...}
  finally{
      closeStatmentand(prepStmt1);
  }
  </code></pre>
  
  <p>Which is the most preferred way to do this ?</p>