title: Java: Insert multiple rows into MySQL with PreparedStatement
body: <p>I want to insert multiple rows into a MySQL table at once using Java. The number of rows is dynamic. In the past I was doing...</p>
  
  <pre><code>for (String element : array) {
      myStatement.setString(1, element[0]);
      myStatement.setString(2, element[1]);
  
      myStatement.executeUpdate();
  }
  </code></pre>
  
  <p>I'd like to optimize this to use the MySQL-supported syntax:</p>
  
  <pre><code>INSERT INTO table (col1, col2) VALUES ('val1', 'val2'), ('val1', 'val2')[, ...]
  </code></pre>
  
  <p>but with a <code>PreparedStatement</code> I don't know of any way to do this since I don't know beforehand how many elements <code>array</code> will contain. If it's not possible with a <code>PreparedStatement</code>, how else can I do it (and still escape the values in the array)?</p>