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

public abstract Class JdbcUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Field, .InvocationTargetException, java.math.BigDecimal, java.sql.Blob, .Clob, .Connection, .DatabaseMetaData, .ResultSet, .ResultSetMetaData, .SQLException, .SQLFeatureNotSupportedException, .Statement, .Types, java.util.HashMap, .Map, javax.sql.DataSource, org.apache.commons.logging.Log, .LogFactory, org.springframework.jdbc.CannotGetJdbcConnectionException, org.springframework.jdbc.datasource.DataSourceUtils, org.springframework.lang.Nullable, org.springframework.util.NumberUtils, .StringUtils

Generic utility methods for working with JDBC. Mainly for internal use within the framework, but also useful for custom JDBC access code.
Authors
Thomas Risberg, Juergen Hoeller, Ben Blinebury

Field Summary

Modifier and TypeField and Description
private static final Log
public static final int
TYPE_UNKNOWN

Constant that indicates an unknown (or unspecified) SQL type.

private static final Map<Integer, String>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
closeConnection(Connection
the JDBC Connection to close (may be null)
con
)

Close the given JDBC Connection and ignore any thrown exception.

public static void
closeResultSet(ResultSet
the JDBC ResultSet to close (may be null)
rs
)

Close the given JDBC ResultSet and ignore any thrown exception.

public static void
closeStatement(Statement
the JDBC Statement to close (may be null)
stmt
)

Close the given JDBC Statement and ignore any thrown exception.

public static String

Returns:

the common name to be used (e.g. "DB2" or "Sybase")
commonDatabaseName
(String
the name as provided in database meta-data
source
)

Extract a common name for the target database in use even if various drivers/platforms provide varying names at runtime.

public static String

Returns:

the column name using underscores
convertPropertyNameToUnderscoreName
(String
the property name to be converted
name
)

Convert a property name using "camelCase" to a corresponding column name with underscores.

public static String

Returns:

the name using "camelCase"
convertUnderscoreNameToPropertyName
(String
the potentially underscores-based column name to be converted
name
)

Convert a column name with underscores to the corresponding property name using "camelCase".

public static <T> T

Returns:

object containing the extracted information, as returned by the DatabaseMetaDataCallback's processMetaData method
extractDatabaseMetaData
(DataSource
the DataSource to extract meta-data for
dataSource
,
DatabaseMetaDataCallback<T>
callback that will do the actual work
action
)

Extract database meta-data via the given DatabaseMetaDataCallback.

public static <T> T

Returns:

the object returned by the specified DatabaseMetaData method
extractDatabaseMetaData
(DataSource
the DataSource to extract meta-data for
dataSource
,
final String
the name of the DatabaseMetaData method to call
metaDataMethodName
)
Deprecated as of 5.2.9, in favor of extractDatabaseMetaData(DataSource, DatabaseMetaDataCallback) with a lambda expression or method reference and a generically typed result

Call the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.

public static Object

Returns:

the value object (possibly not of the specified required type, with further conversion steps necessary)
getResultSetValue
(ResultSet
is the ResultSet holding the data
rs
,
int
is the column index
index
,
Class<?>
the required value type (may be null)
requiredType
)

Retrieve a JDBC column value from a ResultSet, using the specified value type.

public static Object

Returns:

the value object
getResultSetValue
(ResultSet
is the ResultSet holding the data
rs
,
int
is the column index
index
)

Retrieve a JDBC column value from a ResultSet, using the most appropriate value type.

public static boolean

Returns:

whether the type is numeric
isNumeric
(int
the SQL type to be checked
sqlType
)

Check whether the given SQL type is numeric.

public static String

Returns:

the column name to use
lookupColumnName
(ResultSetMetaData
the current meta-data to use
resultSetMetaData
,
int
the index of the column for the lookup
columnIndex
)

Determine the column name to use.

public static String

Returns:

the corresponding constant name in java.sql.Types (e.g. "VARCHAR"/"NUMERIC"), or null if not resolvable
resolveTypeName
(int
the SQL type to resolve
sqlType
)

Resolve the standard type name for the given SQL type, if possible.

public static boolean

Returns:

whether JDBC batch updates are supported
supportsBatchUpdates
(Connection
the Connection to check
con
)

Return whether the given JDBC driver supports JDBC batch updates.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static final Log logger
TYPE_UNKNOWNback to summary
public static final int TYPE_UNKNOWN

Constant that indicates an unknown (or unspecified) SQL type.

See Also
java.sql.Types
typeNamesback to summary
private static final Map<Integer, String> typeNames

Constructor Detail

JdbcUtilsback to summary
public JdbcUtils()

Method Detail

closeConnectionback to summary
public static void closeConnection(Connection con)

Close the given JDBC Connection and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.

Parameters
con:Connection

the JDBC Connection to close (may be null)

closeResultSetback to summary
public static void closeResultSet(ResultSet rs)

Close the given JDBC ResultSet and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.

Parameters
rs:ResultSet

the JDBC ResultSet to close (may be null)

closeStatementback to summary
public static void closeStatement(Statement stmt)

Close the given JDBC Statement and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.

Parameters
stmt:Statement

the JDBC Statement to close (may be null)

commonDatabaseNameback to summary
public static String commonDatabaseName(String source)

Extract a common name for the target database in use even if various drivers/platforms provide varying names at runtime.

Parameters
source:String

the name as provided in database meta-data

Returns:String

the common name to be used (e.g. "DB2" or "Sybase")

Annotations
@Nullable
convertPropertyNameToUnderscoreNameback to summary
public static String convertPropertyNameToUnderscoreName(String name)

Convert a property name using "camelCase" to a corresponding column name with underscores. A name like "customerNumber" would match a "customer_number" column name.

Parameters
name:String

the property name to be converted

Returns:String

the column name using underscores

Since
6.1
See Also
convertUnderscoreNameToPropertyName
convertUnderscoreNameToPropertyNameback to summary
public static String convertUnderscoreNameToPropertyName(String name)

Convert a column name with underscores to the corresponding property name using "camelCase". A name like "customer_number" would match a "customerNumber" property name.

Parameters
name:String

the potentially underscores-based column name to be converted

Returns:String

the name using "camelCase"

See Also
convertPropertyNameToUnderscoreName
extractDatabaseMetaDataback to summary
public static <T> T extractDatabaseMetaData(DataSource dataSource, DatabaseMetaDataCallback<T> action) throws MetaDataAccessException

Extract database meta-data via the given DatabaseMetaDataCallback.

This method will open a connection to the database and retrieve its meta-data. Since this method is called before the exception translation feature is configured for a DataSource, this method can not rely on SQLException translation itself.

Any exceptions will be wrapped in a MetaDataAccessException. This is a checked exception and any calling code should catch and handle this exception. You can just log the error and hope for the best, but there is probably a more serious error that will reappear when you try to access the database again.

Parameters
dataSource:DataSource

the DataSource to extract meta-data for

action:DatabaseMetaDataCallback<T>

callback that will do the actual work

Returns:T

object containing the extracted information, as returned by the DatabaseMetaDataCallback's processMetaData method

Exceptions
MetaDataAccessException:
if meta-data access failed
See Also
java.sql.DatabaseMetaData
extractDatabaseMetaDataback to summary
public static <T> T extractDatabaseMetaData(DataSource dataSource, final String metaDataMethodName) throws MetaDataAccessException

Deprecated

as of 5.2.9, in favor of extractDatabaseMetaData(DataSource, DatabaseMetaDataCallback) with a lambda expression or method reference and a generically typed result

Call the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.

Parameters
dataSource:DataSource

the DataSource to extract meta-data for

metaDataMethodName:String

the name of the DatabaseMetaData method to call

Returns:T

the object returned by the specified DatabaseMetaData method

Annotations
@Deprecated
@SuppressWarnings:unchecked
Exceptions
MetaDataAccessException:
if we couldn't access the DatabaseMetaData or failed to invoke the specified method
See Also
java.sql.DatabaseMetaData
getResultSetValueback to summary
public static Object getResultSetValue(ResultSet rs, int index, Class<?> requiredType) throws SQLException

Retrieve a JDBC column value from a ResultSet, using the specified value type.

Uses the specifically typed ResultSet accessor methods, falling back to getResultSetValue(java.sql.ResultSet, int) for unknown types.

Note that the returned value may not be assignable to the specified required type, in case of an unknown type. Calling code needs to deal with this case appropriately, e.g. throwing a corresponding exception.

Parameters
rs:ResultSet

is the ResultSet holding the data

index:int

is the column index

requiredType:Class<?>

the required value type (may be null)

Returns:Object

the value object (possibly not of the specified required type, with further conversion steps necessary)

Annotations
@Nullable
Exceptions
SQLException:
if thrown by the JDBC API
See Also
getResultSetValue(ResultSet, int)
getResultSetValueback to summary
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException

Retrieve a JDBC column value from a ResultSet, using the most appropriate value type. The returned value should be a detached value object, not having any ties to the active ResultSet: in particular, it should not be a Blob or Clob object but rather a byte array or String representation, respectively.

Uses the getObject(index) method, but includes additional "hacks" to get around Oracle 10g returning a non-standard object for its TIMESTAMP datatype and a java.sql.Date for DATE columns leaving out the time portion: These columns will explicitly be extracted as standard java.sql.Timestamp object.

Parameters
rs:ResultSet

is the ResultSet holding the data

index:int

is the column index

Returns:Object

the value object

Annotations
@Nullable
Exceptions
SQLException:
if thrown by the JDBC API
See Also
java.sql.Blob, java.sql.Clob, java.sql.Timestamp
isNumericback to summary
public static boolean isNumeric(int sqlType)

Check whether the given SQL type is numeric.

Parameters
sqlType:int

the SQL type to be checked

Returns:boolean

whether the type is numeric

lookupColumnNameback to summary
public static String lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex) throws SQLException

Determine the column name to use. The column name is determined based on a lookup using ResultSetMetaData.

This method's implementation takes into account clarifications expressed in the JDBC 4.0 specification:

columnLabel - the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column.

Parameters
resultSetMetaData:ResultSetMetaData

the current meta-data to use

columnIndex:int

the index of the column for the lookup

Returns:String

the column name to use

Exceptions
SQLException:
in case of lookup failure
resolveTypeNameback to summary
public static String resolveTypeName(int sqlType)

Resolve the standard type name for the given SQL type, if possible.

Parameters
sqlType:int

the SQL type to resolve

Returns:String

the corresponding constant name in java.sql.Types (e.g. "VARCHAR"/"NUMERIC"), or null if not resolvable

Annotations
@Nullable
Since
5.2
supportsBatchUpdatesback to summary
public static boolean supportsBatchUpdates(Connection con)

Return whether the given JDBC driver supports JDBC batch updates.

Typically invoked right before execution of a given set of statements: to decide whether the set of SQL statements should be executed through the JDBC batch mechanism or simply in a traditional one-by-one fashion.

Logs a warning if the "supportsBatchUpdates" methods throws an exception and simply returns false in that case.

Parameters
con:Connection

the Connection to check

Returns:boolean

whether JDBC batch updates are supported

See Also
java.sql.DatabaseMetaData#supportsBatchUpdates()