title: Add multiple Prepared Statements in Java sql
body: <p>I have multiple SQL prepared statements I want to execute at the same time. Up until now, I had been using a prepared statement method that looks more or less like this:</p>
  
  <pre><code>    public PreparedStatement createWordEntry(Connection c, String word, String word2, int num) throws SQLException{
          PreparedStatement entry = c.prepareStatement("INSERT INTO table VALUES(?,?,?)");
          entry.setString(1, word);
          entry.setString(2, word2);
          entry.setInt(3,num);
          return entry;
      }
  </code></pre>
  
  <p>Originally, I tried adding these to a vector / array and executing them one at a time, but because of intricacies with resultSets and indexing, I kept getting "jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed"</p>
  
  <p>Then I looked into adding all statements into a batch and executing at once. Of course, addBatch() requires a string as opposed to a PreparedStatement as a parameter. When I searched on this site, I found this answer: <a href="https://stackoverflow.com/questions/5803600/how-to-generate-string-elegantly-in-java">How to generate String &quot;elegantly&quot; in Java?</a> which suggests building and formatting strings to add to a batch leaves code vulnerable to SQL injection attacks.</p>
  
  <p>So, is there an alternative to these two methods for executing multiple queries at once? If there isn't, which of the above is preferable, and why?</p>