title: Multi-row INSERT vs. repeatedly calling PreparedStatement.executeUpdate()
body: <p>If I have a table created with the following schema:</p>
  
  <pre><code>CREATE TABLE names (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR);
  </code></pre>
  
  <p>And a <code>List&lt;String&gt; names</code> of names I want to add to the table, I see two ways of going about this.</p>
  
  <p>The simple, brute force solution is to manually construct the SQL, like so:</p>
  
  <pre><code>String sql = "INSERT INTO names (name) VALUES ("+
               Joiner.on("), (").join(names)+
               ");";
  conn.createStatement().execute(sql);
  </code></pre>
  
  <p>This hits the DB exactly once, and seems intuitive and straightforward.  I'm aware there are SQL-Injection concerns, but for the sake of the question let's assume those are addressed already.</p>
  
  <p>The alternative would be to use a prepared statement, and the <a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html" rel="nofollow">JDBC docs suggest</a> executing that statement repeatedly, like so:</p>
  
  <pre><code>con.setAutoCommit(false);
  PreparedStatement addNames = con.prepareStatement(
     "INSERT INTO names (name) VALUES (?);");
  
  for (String name : names) {
      addNames.setString(1, name);
      addNames.executeUpdate();
      con.commit();
  }
  con.setAutoCommit(true);
  </code></pre>
  
  <p>Is it really the case that executing a <code>PreparedStatement</code> repeatedly like this is more desirable than simply constructing the query String and executing it directly?</p>