title: How do you do an extended insert using JDBC without building strings?
body: <p>I've got an application that parses log files and inserts a huge amount of data into  database. It's written in Java and talks to a MySQL database over JDBC. I've experimented with different ways to insert the data to find the fastest for my particular use case. The one that currently seems to be the best performer is to issue an extended insert (e.g. a single insert with multiple rows), like this:</p>
  
  <pre><code>INSERT INTO the_table (col1, col2, ..., colN) VALUES
  (v1, v2, v3, ..., vN),
  (v1, v2, v3, ..., vN),
  ...,
  (v1, v2, v3, ..., vN);
  </code></pre>
  
  <p>The number of rows can be tens of thousands.</p>
  
  <p>I've tried using prepared statements, but it's nowhere near as fast, probably because each insert is still sent to the DB separately and the tables needs to be locked and whatnot. My colleague who worked on the code before me tried using batching, but that didn't perform well enough either.</p>
  
  <p>The problem is that using extended inserts means that as far as I can tell I need to build the SQL string myself (since the number of rows is variable) and that means that I open up all sorts of SQL injection vectors that I'm no where intelligent enough to find myself. There's got to be a better way to do this.</p>
  
  <p>Obviously I escape the strings I insert, but only with something like <code>str.replace("\"", "\\\"");</code> (repeated for ', ? and \), but I'm sure that isn't enough.</p>