title: How do I insert multilple rows into a DB2 in one statement?
body: <p>I am using Java and jdbc drivers to establish a connection to a DB2 database. I am attempting to insert multiple Rows into a Table Using a single statement.</p>
  
  <p>Below is my Code:</p>
  
  <pre><code> public void createResources(List&lt;Resources&gt; addedResources)
          throws SQLException{
  
      Connection conn = null;
      PreparedStatement statement = null; 
       int i = 0; 
  
       String values = "INSERT INTO GROUPS (GROUP_NAME, ENTRY_NAME, 
  ENTRY_TYPE, LAST_REQ, CREATE_BY, REQ_BY, LAST_CHANGED, LAST_REQ_TIME) VALUES 
   " +  System.getProperty("line.separator");
  
      while(i &lt; addedResources.size()){
  
          Timestamp LAST_CHANGED = 
  Timestamp.valueOf(addedResources.get(i).getLAST_CHANGED().trim());
          Timestamp LAST_REQ_TIME = 
  Timestamp.valueOf(addedResources.get(i).getLAST_REQ_TIME().trim());
  
          values = values + "('" + 
  addedResources.get(i).getGROUP_NAME().trim() + "', '" + 
  
  addedResources.get(i).getENTRY_NAME().trim() + "', '" + 
  
  addedResources.get(i).getENTRY_TYPE().trim() + "', '" +
  
  addedResources.get(i).getLAST_REQ().trim() + "', '"   +
  
  addedResources.get(i).getCREATE_BY().trim() + "', '"  +
  
  addedResources.get(i).getREQ_BY().trim()   + "', '"   +
                                        LAST_CHANGED + "', '"   +
                                        LAST_REQ_TIME + "')," + 
  System.getProperty("line.separator"); 
  
  
      i = i + 1;
      }   
      values = values.substring(0,values.trim().length()-1);
      Log(Integer.toString(values.length()));
      Log(values);
  
      try {
  
           // Get the DB connection
          conn = this.ds.getConnection();
          conn.setAutoCommit(false);
  
          // Prepare the statement and populate with data
          statement = conn.prepareStatement(values);
  
  
  
          // Perform the INSERT operation
          statement.executeUpdate();
  
          //Commit the changes 
          conn.commit();
          Log("Employee Successfully Added!");
      }
      finally{
  
  
          // Any exceptions will be propagated
  
          // Close database objects, regardless of what happened
          if ( statement != null ) {
              statement.close();
          }
          if ( conn != null ) {
              conn.close();
          }
  
      }
  
  
  
     }
  </code></pre>
  
  <p>To summarize, I have a loop that iterates through objects in a List and elongates the insert statement until it contains all rows in the ArrayList. I then clean the end of the String when the loop is complete, and attempt to execute the statement. </p>
  
  <p>Here is a sample of how a query statement will look when the method is executed:</p>
  
  <pre><code>INSERT INTO GROUPS (GROUP_NAME, ENTRY_NAME, ENTRY_TYPE, LAST_REQ, CREATE_BY, 
  REQ_BY, LAST_CHANGED, LAST_REQ_TIME) VALUES 
  ('JOHN', 'TEST1', 'FILE', 'N/A', 'SSDP071', 'N/A', '2018-09-17 
  19:36:34.004', '2018-09-17 19:36:34.004'),
  ('JOHN', 'TEST2', 'FILE', 'N/A', 'SSDP071', 'N/A', '2018-09-17 
  19:36:37.771', '2018-09-17 19:36:37.771'),
  ('JOHN', 'TEST3', 'FILE', 'N/A', 'SSDP071', 'N/A', '2018-09-17 
  19:36:42.021', '2018-09-17 19:36:42.021')
  </code></pre>
  
  <p>My code attempts to execute and throw the following error:</p>
  
  <p>[9/17/18 19:36:42:834 GMT] 00000557 SystemOut                                                    O com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][50053][12311][4.22.37] T2zOS exception: [jcc][T2zos]T2zosPreparedStatement.readPrepareDescribeOutput_:nativePrepareInto:1583: DB2 engine SQL error, SQLCODE = -104, SQLSTATE = 42601, error tokens = ,;FOR  NOT ATOMIC ERRORCODE=-104, SQLSTATE=42601</p>
  
  <p>My error token is a comma and I am uncertain which it is referring to. Is there no way to Insert multiple rows into a DB2 Table? Help would be appreciated!</p>