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

public abstract Class StatementCreatorUtils

extends Object
Class Inheritance
Imports
java.io.StringReader, .StringWriter, java.math.BigDecimal, .BigInteger, java.sql.Blob, .Clob, .DatabaseMetaData, .PreparedStatement, .SQLException, .SQLFeatureNotSupportedException, .Types, java.time.LocalDate, .LocalDateTime, .LocalTime, .OffsetDateTime, .OffsetTime, java.util.Arrays, .Calendar, .Collection, .HashMap, .Map, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.SpringProperties, org.springframework.jdbc.support.SqlValue, org.springframework.lang.Nullable

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.

Authors
Thomas Risberg, Juergen Hoeller
Since
1.1
See Also
PreparedStatementSetter, PreparedStatementCreator, CallableStatementCreator, PreparedStatementCreatorFactory, CallableStatementCreatorFactory, SqlParameter, SqlTypeValue, org.springframework.jdbc.core.support.SqlLobValue

Field Summary

Modifier and TypeField and Description
public static final String
IGNORE_GETPARAMETERTYPE_PROPERTY_NAME

System property that instructs Spring to ignore java.sql.ParameterMetaData#getParameterType completely, i.e. to never even attempt to retrieve PreparedStatement#getParameterMetaData() for StatementCreatorUtils#setNull calls.

private static final Map<Class<?>, Integer>
private static final Log
pack-priv static Boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
cleanupParameters(Object...
parameter values supplied. May be null.
paramValues
)

Clean up all resources held by parameter values which were passed to an execute method.

public static void
cleanupParameters(Collection<?>
parameter values supplied. May be null.
paramValues
)

Clean up all resources held by parameter values which were passed to an execute method.

private static boolean
isDateValue(Class<?> inValueType)

Check whether the given value is a java.util.Date (but not one of the JDBC-specific subclasses).

private static boolean
isStringValue(Class<?> inValueType)

Check whether the given value can be treated as a String value.

public static int

Returns:

the corresponding SQL type, or SqlTypeValue#TYPE_UNKNOWN if none found
javaTypeToSqlParameterType
(Class<?>
the Java type to translate
javaType
)

Derive a default SQL type from the given Java type.

private static void
setNull(PreparedStatement ps, int paramIndex, int sqlType, String typeName)

Set the specified PreparedStatement parameter to null, respecting database-specific peculiarities.

public static void
setParameterValue(PreparedStatement
the prepared statement or callable statement
ps
,
int
index of the parameter we are setting
paramIndex
,
SqlParameter
the parameter as it is declared including type
param
,
Object
the value to set
inValue
)

Set the value for a parameter.

public static void
setParameterValue(PreparedStatement
the prepared statement or callable statement
ps
,
int
index of the parameter we are setting
paramIndex
,
int
the SQL type of the parameter
sqlType
,
Object
the value to set (plain value or an SqlTypeValue)
inValue
)

Set the value for a parameter.

public static void
setParameterValue(PreparedStatement
the prepared statement or callable statement
ps
,
int
index of the parameter we are setting
paramIndex
,
int
the SQL type of the parameter
sqlType
,
String
the type name of the parameter (optional, only used for SQL NULL and SqlTypeValue)
typeName
,
Object
the value to set (plain value or an SqlTypeValue)
inValue
)

Set the value for a parameter.

private static void
setParameterValueInternal(PreparedStatement
the prepared statement or callable statement
ps
,
int
index of the parameter we are setting
paramIndex
,
int
the SQL type of the parameter
sqlType
,
String
the type name of the parameter (optional, only used for SQL NULL and SqlTypeValue)
typeName
,
Integer
the number of digits after the decimal point (for DECIMAL and NUMERIC types)
scale
,
Object
the value to set (plain value or an SqlTypeValue)
inValue
)

Set the value for a parameter.

private static void
setValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Integer scale, Object inValue)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

IGNORE_GETPARAMETERTYPE_PROPERTY_NAMEback to summary
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME

System property that instructs Spring to ignore java.sql.ParameterMetaData#getParameterType completely, i.e. to never even attempt to retrieve PreparedStatement#getParameterMetaData() for StatementCreatorUtils#setNull calls.

The default is "false", trying getParameterType calls first and falling back to PreparedStatement#setNull / PreparedStatement#setObject calls based on well-known behavior of common databases.

Consider switching this flag to "true" if you experience misbehavior at runtime, e.g. with connection pool issues in case of an exception thrown from getParameterType (as reported on JBoss AS 7) or in case of performance problems (as reported on PostgreSQL).

javaTypeToSqlTypeMapback to summary
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap
loggerback to summary
private static final Log logger
shouldIgnoreGetParameterTypeback to summary
pack-priv static Boolean shouldIgnoreGetParameterType
Annotations
@Nullable

Constructor Detail

StatementCreatorUtilsback to summary
public StatementCreatorUtils()

Method Detail

cleanupParametersback to summary
public static void cleanupParameters(Object... paramValues)

Clean up all resources held by parameter values which were passed to an execute method. This is for example important for closing LOB values.

Parameters
paramValues:Object[]

parameter values supplied. May be null.

See Also
DisposableSqlTypeValue#cleanup(), org.springframework.jdbc.core.support.SqlLobValue#cleanup()
cleanupParametersback to summary
public static void cleanupParameters(Collection<?> paramValues)

Clean up all resources held by parameter values which were passed to an execute method. This is for example important for closing LOB values.

Parameters
paramValues:Collection<?>

parameter values supplied. May be null.

See Also
DisposableSqlTypeValue#cleanup(), org.springframework.jdbc.core.support.SqlLobValue#cleanup()
isDateValueback to summary
private static boolean isDateValue(Class<?> inValueType)

Check whether the given value is a java.util.Date (but not one of the JDBC-specific subclasses).

isStringValueback to summary
private static boolean isStringValue(Class<?> inValueType)

Check whether the given value can be treated as a String value.

javaTypeToSqlParameterTypeback to summary
public static int javaTypeToSqlParameterType(Class<?> javaType)

Derive a default SQL type from the given Java type.

Parameters
javaType:Class<?>

the Java type to translate

Returns:int

the corresponding SQL type, or SqlTypeValue#TYPE_UNKNOWN if none found

setNullback to summary
private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, String typeName) throws SQLException

Set the specified PreparedStatement parameter to null, respecting database-specific peculiarities.

setParameterValueback to summary
public static void setParameterValue(PreparedStatement ps, int paramIndex, SqlParameter param, Object inValue) throws SQLException

Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle complex types like arrays and LOBs.

Parameters
ps:PreparedStatement

the prepared statement or callable statement

paramIndex:int

index of the parameter we are setting

param:SqlParameter

the parameter as it is declared including type

inValue:Object

the value to set

Exceptions
SQLException:
if thrown by PreparedStatement methods
setParameterValueback to summary
public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType, Object inValue) throws SQLException

Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle complex types like arrays and LOBs.

Parameters
ps:PreparedStatement

the prepared statement or callable statement

paramIndex:int

index of the parameter we are setting

sqlType:int

the SQL type of the parameter

inValue:Object

the value to set (plain value or an SqlTypeValue)

Exceptions
SQLException:
if thrown by PreparedStatement methods
See Also
SqlTypeValue
setParameterValueback to summary
public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Object inValue) throws SQLException

Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle complex types like arrays and LOBs.

Parameters
ps:PreparedStatement

the prepared statement or callable statement

paramIndex:int

index of the parameter we are setting

sqlType:int

the SQL type of the parameter

typeName:String

the type name of the parameter (optional, only used for SQL NULL and SqlTypeValue)

inValue:Object

the value to set (plain value or an SqlTypeValue)

Exceptions
SQLException:
if thrown by PreparedStatement methods
See Also
SqlTypeValue
setParameterValueInternalback to summary
private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Integer scale, Object inValue) throws SQLException

Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle complex types like arrays and LOBs.

Parameters
ps:PreparedStatement

the prepared statement or callable statement

paramIndex:int

index of the parameter we are setting

sqlType:int

the SQL type of the parameter

typeName:String

the type name of the parameter (optional, only used for SQL NULL and SqlTypeValue)

scale:Integer

the number of digits after the decimal point (for DECIMAL and NUMERIC types)

inValue:Object

the value to set (plain value or an SqlTypeValue)

Exceptions
SQLException:
if thrown by PreparedStatement methods
See Also
SqlTypeValue
setValueback to summary
private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Integer scale, Object inValue) throws SQLException