Java Collection Create createNamedInsert(String tableName, Collection columnNames)

Here you can find the source of createNamedInsert(String tableName, Collection columnNames)

Description

Create an insert SQL string for the table with the specified insert columns.

License

Open Source License

Parameter

Parameter Description
tableName Table name.
columnNames Column names for insert.

Return

An insert SQL statement with the names (suitable for passing to Spring named parameter template).

Declaration

public static String createNamedInsert(String tableName, Collection<String> columnNames) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * /*from  w  w w.  j  av  a 2 s . c om*/
 * Copyright (C) Zenoss, Inc. 2010, all rights reserved.
 * 
 * This content is made available according to terms specified in
 * License.zenoss under the directory where your Zenoss product is installed.
 * 
 ****************************************************************************/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Create an insert SQL string for the table with the specified insert columns.
     *
     * @param tableName Table name.
     * @param columnNames Column names for insert.
     * @return An insert SQL statement with the names (suitable for passing to Spring named
     *         parameter template).
     */
    public static String createNamedInsert(String tableName, Collection<String> columnNames) {
        StringBuilder names = new StringBuilder();
        StringBuilder values = new StringBuilder();
        Iterator<String> it = columnNames.iterator();
        while (it.hasNext()) {
            final String columnName = it.next();
            names.append(columnName);
            values.append(':').append(columnName);
            if (it.hasNext()) {
                names.append(',');
                values.append(',');
            }
        }
        return "INSERT INTO " + tableName + " (" + names + ") VALUES (" + values + ")";
    }
}

Related

  1. createCsvString(Collection in)
  2. createFloatArray(java.util.Collection collection)
  3. createInstance( Class collectionType)
  4. createIntArray(Collection collection)
  5. createIntegerInClause(Collection p_integers)
  6. createNewValue(String value, Collection values)
  7. createNominalDescription(Collection values)
  8. createParameterizedWhereClause(Collection columns)
  9. createStringCollection(Class clazz, String... elements)