title: JDBC PreparedStatement Batch continue insert on error
body: <p>Hello guy I create a Batch with a <code>PreparedStatement</code> in java like this</p>
  
  <pre><code>for(Item  item: list){
      ps.setString(1, item.getSome());
      ps.setString(2, item.getFoo());
      ps.setString(3, item.getBatman());
      statement.addBatch();
  
      if (++count % batchSize == 0) {
          results = ps.executeBatch(); //execute parcial batch
  
          if (results != null)
             System.out.println(results.length);
      }
  
  }
  results= ps.executeBatch(); //execute rest of batch
  </code></pre>
  
  <p>The datebase server is a MySQL, in table to insert I have several restrictions</p>
  
  <p>By these restrictions when I insert generates errors</p>
  
  <p><strong>I want run the batch and omit errors</strong>, at this moment throw a Exception a ends batch</p>
  
  <p>Before I create the batch I have a Big for the save one by one like</p>
  
  <pre><code>//seudocode level
  For item
  Try{
     insert item
  }catch(E){nothing happens}
  </code></pre>
  
  <p>But it is very slow, in some cases, the batch procces 4000 item, insert 1500 and omit the rest</p>
  
  <p>How do I do with the batch?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I use weblogic to make conections with this driver <code>mysql-connector-java-commercial-5.0.3-bin</code></p>
  
  <p>I test this properties</p>
  
  <p>1.</p>
  
  <pre><code>continueBatchOnError=true
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>rewriteBatchedStatements=true
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>continueBatchOnError=true
  rewriteBatchedStatements=true
  </code></pre>
  
  <p>And add <code>connection.setAutoCommit(false);</code> but continues throw the exception in duplicates</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>forgot to mention, I use for connection Hibernate + Spring</p>
  
  <p>The only For-Save example is made in Hibernate, but for performance i tried use a JDBC Batch, in other procces in the webapp also use JDBC with the connection from Hibernate and works well</p>
  
  <p>This is the full code </p>
  
  <pre><code>@Transactional
  public void saveMany(final List&lt;Item&gt; items) {
      getMySqlSession().doWork(new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
  
              StringBuilder sb = new StringBuilder();
              sb.append("INSERT INTO `FRT_DB`.`ITEM` ");
              sb.append("( ");
              sb.append("`masterID`, ");
              sb.append("`agent`, ");
              sb.append("`rangeID`) ");
              sb.append("VALUES ");
              sb.append("( ");
              sb.append("?, ");
              sb.append("?, ");
              sb.append("?) ");
  
              int[] results = null;
  
              PreparedStatement ps = null;
  
              try {
                  connection.setAutoCommit(false);
                  ps = connection.prepareStatement(sb.toString());
  
  
                  final int batchSize = 250;
                  int count = 0;
  
                  for (Item item : items) {
  
                      if (item.getMasterId() != null) {
                          ps.setInt(1, item.getMasterId());
                      } else
                          ps.setNull(1, java.sql.Types.INTEGER);
  
                      if (item.getAgent() != null) {
                          ps.setString(2, item.getAgent());
                      } else
                          ps.setNull(2, Types.VARCHAR);
  
                      if (item.getRangeId() != null)
                          ps.setInt(3, item.getRangeId());
                      else
                          ps.setNull(3, Types.INTEGER);
  
                      ps.addBatch();
  
                      if (++count % batchSize == 0) {
                          results = ps.executeBatch();
  
                          if (results != null)
                              System.out.println(results.length);
  
                      }
  
                  }
  
                  results= ps.executeBatch();
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
          }
      });
  }
  </code></pre>
  
  <p>This produce next Exception</p>
  
  <blockquote>
    <p>java.sql.BatchUpdateException: Duplicate entry '1-000002725' for key
    'masterID'</p>
  </blockquote>
  
  <p>But I need to continue</p>
  
  <p>spring + hibernate settings interfere with the properties of jdbc? I dont know</p>