Top Description Fields Constructors Methods
org.springframework.jdbc.core.simple

public Class SimpleJdbcInsert

extends AbstractJdbcInsert
implements SimpleJdbcInsertOperations
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations
Imports
java.util.Arrays, .Map, javax.sql.DataSource, org.springframework.jdbc.core.JdbcTemplate, org.springframework.jdbc.core.namedparam.SqlParameterSource, org.springframework.jdbc.support.KeyHolder

A SimpleJdbcInsert is a multi-threaded, reusable object providing easy (batch) insert capabilities for a table. It provides meta-data processing to simplify the code needed to construct a basic insert statement. All you need to provide is the name of the table and a Map containing the column names and the column values.

The meta-data processing is based on the DatabaseMetaData provided by the JDBC driver. As long as the JDBC driver can provide the names of the columns for a specified table then we can rely on this auto-detection feature. If that is not the case, then the column names must be specified explicitly.

The actual (batch) insert is handled using Spring's JdbcTemplate.

Many of the configuration methods return the current instance of the SimpleJdbcInsert to provide the ability to chain multiple ones together in a "fluent" API style.

Authors
Thomas Risberg, Juergen Hoeller, Sam Brannen
Since
2.5
See Also
java.sql.DatabaseMetaData, org.springframework.jdbc.core.JdbcTemplate

Field Summary

Inherited from org.springframework.jdbc.core.simple.AbstractJdbcInsert:
logger

Constructor Summary

AccessConstructor and Description
public
SimpleJdbcInsert(DataSource
the DataSource to use
dataSource
)

Constructor that accepts the JDBC DataSource to use when creating the JdbcTemplate.

public
SimpleJdbcInsert(JdbcTemplate
the JdbcTemplate to use
jdbcTemplate
)

Alternative constructor that accepts the JdbcTemplate to be used.

Method Summary

Modifier and TypeMethod and Description
public int
execute(Map<String, ?>
a Map containing column names and corresponding value
args
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Execute the insert using the values passed in.

public int
execute(SqlParameterSource
the SqlParameterSource containing values to use for insert
parameterSource
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Execute the insert using the values passed in.

public Number
executeAndReturnKey(Map<String, ?>
a Map containing column names and corresponding value
args
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Execute the insert using the values passed in and return the generated key.

public Number
executeAndReturnKey(SqlParameterSource
the SqlParameterSource containing values to use for insert
parameterSource
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Execute the insert using the values passed in and return the generated key.

public KeyHolder
executeAndReturnKeyHolder(Map<String, ?>
a Map containing column names and corresponding value
args
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Execute the insert using the values passed in and return the generated keys.

public KeyHolder
executeAndReturnKeyHolder(SqlParameterSource
the SqlParameterSource containing values to use for insert
parameterSource
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Execute the insert using the values passed in and return the generated keys.

public int[]
executeBatch(Map<String, ?>...
an array of Maps containing a batch of column names and corresponding value
batch
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Execute a batch insert using the batch of values passed in.

public int[]
executeBatch(SqlParameterSource...
an array of SqlParameterSource containing values for the batch
batch
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Execute a batch insert using the batch of values passed in.

public SimpleJdbcInsert
public SimpleJdbcInsert
usingColumns(String...
one or more column names
columnNames
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingColumns.

Specify the column names that the insert statement should be limited to use.

public SimpleJdbcInsert
usingGeneratedKeyColumns(String...
one or more column names
columnNames
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingGeneratedKeyColumns.

Specify the names of any columns that have auto-generated keys.

public SimpleJdbcInsert
public SimpleJdbcInsert
withCatalogName(String
the name of the catalog
catalogName
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withCatalogName.

Specify the catalog name, if any, to be used for the insert.

public SimpleJdbcInsert
withoutTableColumnMetaDataAccess()

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withoutTableColumnMetaDataAccess.

Turn off any processing of column meta-data information obtained via JDBC.

public SimpleJdbcInsert
withSchemaName(String
the name of the schema
schemaName
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withSchemaName.

Specify the schema name, if any, to be used for the insert.

public SimpleJdbcInsert
withTableName(String
the name of the stored table
tableName
)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withTableName.

Specify the table name to be used for the insert.

Inherited from org.springframework.jdbc.core.simple.AbstractJdbcInsert:
checkCompiledcheckIfConfigurationModificationIsAllowedcompilecompileInternaldoExecutedoExecutedoExecuteAndReturnKeydoExecuteAndReturnKeydoExecuteAndReturnKeyHolderdoExecuteAndReturnKeyHolderdoExecuteBatchdoExecuteBatchgetCatalogNamegetColumnNamesgetGeneratedKeyNamesgetInsertStringgetInsertTypesgetJdbcTemplategetSchemaNamegetTableNameisCompiledisQuoteIdentifiersmatchInParameterValuesWithInsertColumnsmatchInParameterValuesWithInsertColumnsonCompileInternalsetAccessTableColumnMetaDatasetCatalogNamesetColumnNamessetGeneratedKeyNamesetGeneratedKeyNamessetOverrideIncludeSynonymsDefaultsetQuoteIdentifierssetSchemaNamesetTableName

Constructor Detail

SimpleJdbcInsertback to summary
public SimpleJdbcInsert(DataSource dataSource)

Constructor that accepts the JDBC DataSource to use when creating the JdbcTemplate.

Parameters
dataSource:DataSource

the DataSource to use

See Also
org.springframework.jdbc.core.JdbcTemplate#setDataSource
SimpleJdbcInsertback to summary
public SimpleJdbcInsert(JdbcTemplate jdbcTemplate)

Alternative constructor that accepts the JdbcTemplate to be used.

Parameters
jdbcTemplate:JdbcTemplate

the JdbcTemplate to use

See Also
org.springframework.jdbc.core.JdbcTemplate#setDataSource

Method Detail

executeback to summary
public int execute(Map<String, ?> args)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Execute the insert using the values passed in.

Parameters
args:Map<String, ?>

a Map containing column names and corresponding value

Returns:int

the number of rows affected as returned by the JDBC driver

Annotations
@Override
executeback to summary
public int execute(SqlParameterSource parameterSource)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.execute.

Execute the insert using the values passed in.

Parameters
parameterSource:SqlParameterSource

the SqlParameterSource containing values to use for insert

Returns:int

the number of rows affected as returned by the JDBC driver

Annotations
@Override
executeAndReturnKeyback to summary
public Number executeAndReturnKey(Map<String, ?> args)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Execute the insert using the values passed in and return the generated key.

This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

Parameters
args:Map<String, ?>

a Map containing column names and corresponding value

Returns:Number

the generated key value

Annotations
@Override
executeAndReturnKeyback to summary
public Number executeAndReturnKey(SqlParameterSource parameterSource)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKey.

Execute the insert using the values passed in and return the generated key.

This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

Parameters
parameterSource:SqlParameterSource

the SqlParameterSource containing values to use for insert

Returns:Number

the generated key value.

Annotations
@Override
executeAndReturnKeyHolderback to summary
public KeyHolder executeAndReturnKeyHolder(Map<String, ?> args)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Execute the insert using the values passed in and return the generated keys.

This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

Parameters
args:Map<String, ?>

a Map containing column names and corresponding value

Returns:KeyHolder

the KeyHolder containing all generated keys

Annotations
@Override
executeAndReturnKeyHolderback to summary
public KeyHolder executeAndReturnKeyHolder(SqlParameterSource parameterSource)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeAndReturnKeyHolder.

Execute the insert using the values passed in and return the generated keys.

This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

Parameters
parameterSource:SqlParameterSource

the SqlParameterSource containing values to use for insert

Returns:KeyHolder

the KeyHolder containing all generated keys

Annotations
@Override
executeBatchback to summary
public int[] executeBatch(Map<String, ?>... batch)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Execute a batch insert using the batch of values passed in.

Parameters
batch:Map<String, ?>[]

an array of Maps containing a batch of column names and corresponding value

Returns:int[]

the array of number of rows affected as returned by the JDBC driver

Annotations
@SuppressWarnings:unchecked
@Override
executeBatchback to summary
public int[] executeBatch(SqlParameterSource... batch)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.executeBatch.

Execute a batch insert using the batch of values passed in.

Parameters
batch:SqlParameterSource[]

an array of SqlParameterSource containing values for the batch

Returns:int[]

the array of number of rows affected as returned by the JDBC driver

Annotations
@Override
includeSynonymsForTableColumnMetaDataback to summary
public SimpleJdbcInsert includeSynonymsForTableColumnMetaData()

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.includeSynonymsForTableColumnMetaData.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.includeSynonymsForTableColumnMetaData.

Include synonyms for the column meta-data lookups via JDBC.

Note

This is only necessary to include for Oracle since other databases supporting synonyms seem to include the synonyms automatically.

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
usingColumnsback to summary
public SimpleJdbcInsert usingColumns(String... columnNames)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingColumns.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingColumns.

Specify the column names that the insert statement should be limited to use.

Parameters
columnNames:String[]

one or more column names

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
usingGeneratedKeyColumnsback to summary
public SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingGeneratedKeyColumns.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingGeneratedKeyColumns.

Specify the names of any columns that have auto-generated keys.

Parameters
columnNames:String[]

one or more column names

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
usingQuotedIdentifiersback to summary
public SimpleJdbcInsert usingQuotedIdentifiers()

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingQuotedIdentifiers.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.usingQuotedIdentifiers.

Specify that SQL identifiers should be quoted.

If this method is invoked, the identifier quote string for the underlying database will be used to quote SQL identifiers in generated SQL statements. In this context, SQL identifiers refer to schema, table, and column names.

When identifiers are quoted, explicit column names must be supplied via usingColumns(String...). Furthermore, all identifiers for the schema name, table name, and column names must match the corresponding identifiers in the database's metadata regarding casing (mixed case, uppercase, or lowercase).

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
withCatalogNameback to summary
public SimpleJdbcInsert withCatalogName(String catalogName)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withCatalogName.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withCatalogName.

Specify the catalog name, if any, to be used for the insert.

Parameters
catalogName:String

the name of the catalog

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
withoutTableColumnMetaDataAccessback to summary
public SimpleJdbcInsert withoutTableColumnMetaDataAccess()

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withoutTableColumnMetaDataAccess.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withoutTableColumnMetaDataAccess.

Turn off any processing of column meta-data information obtained via JDBC.

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
withSchemaNameback to summary
public SimpleJdbcInsert withSchemaName(String schemaName)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withSchemaName.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withSchemaName.

Specify the schema name, if any, to be used for the insert.

Parameters
schemaName:String

the name of the schema

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override
withTableNameback to summary
public SimpleJdbcInsert withTableName(String tableName)

Implements org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withTableName.

Doc from org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations.withTableName.

Specify the table name to be used for the insert.

Parameters
tableName:String

the name of the stored table

Returns:SimpleJdbcInsert

this SimpleJdbcInsert (for method chaining)

Annotations
@Override