title: Insert a HashMap into any database table
body: <p>I have a HashMap with the following definition:</p>
  
  <pre><code>  myMap = new HashMap&lt;String,String&gt;();
  </code></pre>
  
  <p>this map consists of the field names as keys and field values as of course values. I am trying to make a method that takes the HashMap and a table name as arguments. My query has to have the following format because I do not insert to all the columns in my table:</p>
  
  <pre><code>INSERT INTO $tableName (?,?,?,?)
  VALUES (?,?,?,?)
  </code></pre>
  
  <p>The number of columns of course depends on the size of the HashMap. 
  How can I achieve this through iterating through the HashMap.
  Here is what I have come up so far using a different approach but I do not think it will work properly:</p>
  
  <pre><code>public void insertData(HashMap&lt;String, String&gt; dataMap, String tableName) {
  
      int size=dataMap.size();
      String sql = "INSERT INTO " + tableName;
      Iterator&lt;Entry&lt;String, String&gt;&gt; it = dataMap.entrySet().iterator();
      int counter = 1;
      while (it.hasNext()) {
          Map.Entry pairs = (Map.Entry)it.next();
          sql += pairs.getKey()+"="+pairs.getValue();
          if(size &gt; counter )
              sql += ", ";
          counter++;
      }
      sql += ";";
  }
  </code></pre>