title: JDBC: Create Table with PreparedStatement, SQL Syntax Error or not?
body: <p>I am encountering some difficulties trying to, in a dynamic way, create a new table using PreparedStatement. I am trying to add a unknown number of columns and their respective types and sizes. I keep getting SQL Syntax errors, but I suspect this may not be the case here.</p>
  
  <p>Variables used:</p>
  
  <ul>
  <li><strong>con</strong> = Connection object</li>
  <li><strong>colNames</strong> = String[] containing column names ("person", "email" etc)</li>
  <li><strong>colTypes</strong> = String[] containing column types ("varchar", "int" etc)</li>
  <li><strong>colSizes</strong> = String[] containing column sizes ("100", "11", etc)</li>
  </ul>
  
  <p>Should be pretty self-explanatory.</p>
  
  <pre><code>con.setAutoCommit(false);
  String sql = "? ?(?),";
  PreparedStatement ps = con.prepareStatement(sql);
  ps.addBatch("create table " + tablename + "( ");
  
  for (int i = 0; i &lt; colNames.length; i++){
  
      if (!(i == colNames.length-1)) {
          ps.setString(1, colNames[i]);
          ps.setString(2, colTypes[i]);
          ps.setString(3, colSizes[i]);
  
      } else {
          String format = "%s %s(%s)";
          String lastLine = String.format(format, colNames[i], colTypes[i], colSizes[i]);
          ps.addBatch(lastLine);
  
      }
  }
  
  ps.addBatch(");");
  ps.executeBatch();
  </code></pre>
  
  <p>NOTE: Yes, this is homework. I don't want any dead giveaways, rather pointers as to in what way I am misusing some functions, which I suspect.</p>
  
  <p>Best regards,
  Krys</p>