spring-framework / org.springframework.jdbc.core

Package org.springframework.jdbc.core

Types

ArgumentPreparedStatementSetter

open class ArgumentPreparedStatementSetter : PreparedStatementSetter, ParameterDisposer

Simple adapter for PreparedStatementSetter that applies a given array of arguments.

ArgumentTypePreparedStatementSetter

open class ArgumentTypePreparedStatementSetter : PreparedStatementSetter, ParameterDisposer

Simple adapter for PreparedStatementSetter that applies given arrays of arguments and JDBC argument types.

BatchUpdateUtils

abstract class BatchUpdateUtils

Generic utility methods for working with JDBC batch statements. Mainly for internal use within the framework.

BeanPropertyRowMapper

open class BeanPropertyRowMapper<T : Any> : RowMapper<T>

RowMapper implementation that converts a row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.

Column values are mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties. The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.

Mapping is provided for fields in the target class for many common types, e.g.: String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long, float, Float, double, Double, BigDecimal, java.util.Date, etc.

To facilitate mapping between columns and fields that don't have matching names, try using column aliases in the SQL statement like "select fname as first_name from customer".

For 'null' values read from the database, we will attempt to call the setter, but in the case of Java primitives, this causes a TypeMismatchException. This class can be configured (using the primitivesDefaultedForNullValue property) to trap this exception and use the primitives default value. Be aware that if you use the values from the generated bean to update the database the primitive value will have been set to the primitive's default value instead of null.

Please note that this class is designed to provide convenience rather than high performance. For best performance, consider using a custom RowMapper implementation.

CallableStatementCreatorFactory

open class CallableStatementCreatorFactory

Helper class that efficiently creates multiple CallableStatementCreator objects with different parameters based on a SQL statement and a single set of parameter declarations.

ColumnMapRowMapper

open class ColumnMapRowMapper : RowMapper<MutableMap<String, Any>>

RowMapper implementation that creates a java.util.Map for each row, representing all columns as key-value pairs: one entry for each column, with the column name as key.

The Map implementation to use and the key to use for each column in the column Map can be customized through overriding #createColumnMap and #getColumnKey, respectively.

Note: By default, ColumnMapRowMapper will try to build a linked Map with case-insensitive keys, to preserve column order as well as allow any casing to be used for column names. This requires Commons Collections on the classpath (which will be autodetected). Else, the fallback is a standard linked HashMap, which will still preserve column order but requires the application to specify the column names in the same casing as exposed by the driver.

PreparedStatementCreatorFactory

open class PreparedStatementCreatorFactory

Helper class that efficiently creates multiple PreparedStatementCreator objects with different parameters based on a SQL statement and a single set of parameter declarations.

ResultSetSupportingSqlParameter

open class ResultSetSupportingSqlParameter : SqlParameter

Common base class for ResultSet-supporting SqlParameters like SqlOutParameter and SqlReturnResultSet.

RowCountCallbackHandler

open class RowCountCallbackHandler : RowCallbackHandler

Implementation of RowCallbackHandler. Convenient superclass for callback handlers. An instance can only be used once.

We can either use this on its own (for example, in a test case, to ensure that our result sets have valid dimensions), or use it as a superclass for callback handlers that actually do something, and will benefit from the dimension information it provides.

A usage example with JdbcTemplate:

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object RowCountCallbackHandler countCallback = new RowCountCallbackHandler(); // not reusable jdbcTemplate.query("select * from user", countCallback); int rowCount = countCallback.getRowCount();

RowMapperResultSetExtractor

open class RowMapperResultSetExtractor<T : Any> : ResultSetExtractor<MutableList<T>>

Adapter implementation of the ResultSetExtractor interface that delegates to a RowMapper which is supposed to create an object for each row. Each object is added to the results List of this ResultSetExtractor.

Useful for the typical case of one object per row in the database table. The number of entries in the results list will match the number of rows.

Note that a RowMapper object is typically stateless and thus reusable; just the RowMapperResultSetExtractor adapter is stateful.

A usage example with JdbcTemplate:

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object RowMapper rowMapper = new UserRowMapper(); // reusable object List allUsers = (List) jdbcTemplate.query( "select * from user", new RowMapperResultSetExtractor(rowMapper, 10)); User user = (User) jdbcTemplate.queryForObject( "select * from user where id=?", new Object[] {id}, new RowMapperResultSetExtractor(rowMapper, 1));

Alternatively, consider subclassing MappingSqlQuery from the jdbc.object package: Instead of working with separate JdbcTemplate and RowMapper objects, you can have executable query objects (containing row-mapping logic) there.

SingleColumnRowMapper

open class SingleColumnRowMapper<T : Any> : RowMapper<T>

RowMapper implementation that converts a single column into a single result value per row. Expects to operate on a java.sql.ResultSet that just contains a single column.

The type of the result value for each row can be specified. The value for the single column will be extracted from the ResultSet and converted into the specified target type.

SqlInOutParameter

open class SqlInOutParameter : SqlOutParameter

Subclass of SqlOutParameter to represent an INOUT parameter. Will return true for SqlParameter's #isInputValueProvided test, in contrast to a standard SqlOutParameter.

Output parameters - like all stored procedure parameters - must have names.

SqlOutParameter

open class SqlOutParameter : ResultSetSupportingSqlParameter

Subclass of SqlParameter to represent an output parameter. No additional properties: instanceof will be used to check for such types.

Output parameters - like all stored procedure parameters - must have names.

SqlParameterValue

open class SqlParameterValue : SqlParameter

Object to represent a SQL parameter value, including parameter metadata such as the SQL type and the scale for numeric values.

Designed for use with JdbcTemplate's operations that take an array of argument values: Each such argument value may be a SqlParameterValue, indicating the SQL type (and optionally the scale) instead of letting the template guess a default type. Note that this only applies to the operations with a 'plain' argument array, not to the overloaded variants with an explicit type array.

SqlProvider

interface SqlProvider

Interface to be implemented by objects that can provide SQL strings.

Typically implemented by PreparedStatementCreators, CallableStatementCreators and StatementCallbacks that want to expose the SQL they use to create their statements, to allow for better contextual information in case of exceptions.

SqlReturnUpdateCount

open class SqlReturnUpdateCount : SqlParameter

Represents a returned update count from a stored procedure call.

Returned update counts - like all stored procedure parameters - must have names.

SqlRowSetResultSetExtractor

open class SqlRowSetResultSetExtractor : ResultSetExtractor<SqlRowSet>

ResultSetExtractor implementation that returns a Spring SqlRowSet representation for each given ResultSet.

The default implementation uses a standard JDBC CachedRowSet underneath.

StatementCreatorUtils

abstract class StatementCreatorUtils

Utility methods for PreparedStatementSetter/Creator and CallableStatementCreator implementations, providing sophisticated parameter management (including support for LOB values).

Used by PreparedStatementCreatorFactory and CallableStatementCreatorFactory, but also available for direct use in custom setter/creator implementations.

Functions

query

fun <T : Any> JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet) -> T): T?

Extension for JdbcOperations.query providing a ResultSetExtractor-like function variant: query<Foo>("...", arg1, argN){ rs -> }.

fun JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet) -> Unit): Unit

Extension for JdbcOperations.query providing a RowCallbackHandler-like function variant: query("...", arg1, argN){ rs -> }.

fun <T : Any> JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): List<T>

Extensions for JdbcOperations.query providing a RowMapper-like function variant: query("...", arg1, argN){ rs, i -> }.

queryForList

fun <T : Any> JdbcOperations.queryForList(sql: String): List<T>

Extension for JdbcOperations.queryForList providing a queryForList<Foo>("...") variant.

fun <T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, argTypes: IntArray): List<T>

Extension for JdbcOperations.queryForList providing a queryForList<Foo>("...", arrayOf(arg1, argN), intArray(type1, typeN)) variant.

fun <T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>): List<T>

Extension for JdbcOperations.queryForList providing a queryForList<Foo>("...", arrayOf(arg1, argN)) variant.

queryForObject

fun <T : Any> JdbcOperations.queryForObject(sql: String): T?

Extension for JdbcOperations.queryForObject providing a queryForObject<Foo>("...") variant.

fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T?

Extensions for JdbcOperations.queryForObject providing a RowMapper-like function variant: queryForObject("...", arg1, argN){ rs, i -> }.

fun <T : Any> JdbcOperations.queryForObject(sql: String, args: Array<out Any>, argTypes: IntArray): T?

Extension for JdbcOperations.queryForObject providing a queryForObject<Foo>("...", arrayOf(arg1, argN), intArray(type1, typeN)) variant.

fun <T : Any> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): T?

Extension for JdbcOperations.queryForObject providing a queryForObject<Foo>("...", arrayOf(arg1, argN)) variant.