title: Dynamically add columns and column names Jdbc/MySql
body: <p>Is there a way to add dynamically columns and column names to an existing table using jdbc?</p>
  
  <p>For example: </p>
  
  <p>If <code>NumberOfColumns = 3</code>, I want the column names to be "<code>Column1</code>", <code>Column2</code>", "<code>Column3</code>".</p>
  
  <p>I tried to add dynamically some columns with the name of iterator just for testing my code but it gives me an SQL syntax error.</p>
  
  <p>Below are some parts of my code I just described. If I remove the whole loop, the code works like a charm.</p>
  
  <pre><code>public class something {
  
      //Some Variables Declaration///
      //Number of columns in test table//
      int NumberOfColumns = 3;
  
      public static void main(String args[]) {
  
          //..... SOME CODE....//       
          //Create database//
          sql = "CREATE DATABASE mydb";
          stmt.executeUpdate(sql);
  
          //Create test table// 
          sql = "CREATE TABLE mydb.table "
                  + "(id INTEGER not NULL ";
  
          stmt.executeUpdate(sql);
  
          //Add columns dynamically//
          for (int i = 0; i &lt; NumberOfColumns; i++) {
  
              sql = "ALTER TABLE mydb.test ADD'" + i + "' VARCHAR(30)";
              stmt.executeUpdate(sql);
          }
          stmt.executeUpdate(sql);
      }
  }
  </code></pre>