Class JdbcUtils
- Author:
- Thomas Risberg, Juergen Hoeller, Ben Blinebury
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Constant that indicates an unknown (or unspecified) SQL type. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Close the given JDBC Connection and ignore any thrown exception.static void
Close the given JDBC ResultSet and ignore any thrown exception.static void
closeStatement
(Statement stmt) Close the given JDBC Statement and ignore any thrown exception.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.static String
Convert a property name using "camelCase" to a corresponding column name with underscores.static String
Convert a column name with underscores to the corresponding property name using "camelCase".static <T> T
extractDatabaseMetaData
(DataSource dataSource, String metaDataMethodName) Deprecated.static <T> T
extractDatabaseMetaData
(DataSource dataSource, DatabaseMetaDataCallback<T> action) Extract database meta-data via the given DatabaseMetaDataCallback.static Object
getResultSetValue
(ResultSet rs, int index) Retrieve a JDBC column value from a ResultSet, using the most appropriate value type.static Object
getResultSetValue
(ResultSet rs, int index, Class<?> requiredType) Retrieve a JDBC column value from a ResultSet, using the specified value type.static boolean
isNumeric
(int sqlType) Check whether the given SQL type is numeric.static String
lookupColumnName
(ResultSetMetaData resultSetMetaData, int columnIndex) Determine the column name to use.static String
resolveTypeName
(int sqlType) Resolve the standard type name for the given SQL type, if possible.static boolean
Return whether the given JDBC driver supports JDBC 2.0 batch updates.
-
Field Details
-
TYPE_UNKNOWN
public static final int TYPE_UNKNOWNConstant that indicates an unknown (or unspecified) SQL type.- See Also:
-
-
Constructor Details
-
JdbcUtils
public JdbcUtils()
-
-
Method Details
-
closeConnection
Close the given JDBC Connection and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.- Parameters:
con
- the JDBC Connection to close (may benull
)
-
closeStatement
Close the given JDBC Statement and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.- Parameters:
stmt
- the JDBC Statement to close (may benull
)
-
closeResultSet
Close the given JDBC ResultSet and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.- Parameters:
rs
- the JDBC ResultSet to close (may benull
)
-
getResultSetValue
@Nullable public static Object getResultSetValue(ResultSet rs, int index, @Nullable 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
- is the ResultSet holding the dataindex
- is the column indexrequiredType
- the required value type (may benull
)- Returns:
- the value object (possibly not of the specified required type, with further conversion steps necessary)
- Throws:
SQLException
- if thrown by the JDBC API- See Also:
-
getResultSetValue
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 ajava.sql.Date
for DATE columns leaving out the time portion: These columns will explicitly be extracted as standardjava.sql.Timestamp
object.- Parameters:
rs
- is the ResultSet holding the dataindex
- is the column index- Returns:
- the value object
- Throws:
SQLException
- if thrown by the JDBC API- See Also:
-
extractDatabaseMetaData
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
- the DataSource to extract meta-data foraction
- callback that will do the actual work- Returns:
- object containing the extracted information, as returned by
the DatabaseMetaDataCallback's
processMetaData
method - Throws:
MetaDataAccessException
- if meta-data access failed- See Also:
-
extractDatabaseMetaData
@Deprecated public static <T> T extractDatabaseMetaData(DataSource dataSource, String metaDataMethodName) throws MetaDataAccessException Deprecated.as of 5.2.9, in favor ofextractDatabaseMetaData(DataSource, DatabaseMetaDataCallback)
with a lambda expression or method reference and a generically typed resultCall the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.- Parameters:
dataSource
- the DataSource to extract meta-data formetaDataMethodName
- the name of the DatabaseMetaData method to call- Returns:
- the object returned by the specified DatabaseMetaData method
- Throws:
MetaDataAccessException
- if we couldn't access the DatabaseMetaData or failed to invoke the specified method- See Also:
-
supportsBatchUpdates
Return whether the given JDBC driver supports JDBC 2.0 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 2.0 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
- the Connection to check- Returns:
- whether JDBC 2.0 batch updates are supported
- See Also:
-
commonDatabaseName
Extract a common name for the target database in use even if various drivers/platforms provide varying names at runtime.- Parameters:
source
- the name as provided in database meta-data- Returns:
- the common name to be used (e.g. "DB2" or "Sybase")
-
isNumeric
public static boolean isNumeric(int sqlType) Check whether the given SQL type is numeric.- Parameters:
sqlType
- the SQL type to be checked- Returns:
- whether the type is numeric
-
resolveTypeName
Resolve the standard type name for the given SQL type, if possible.- Parameters:
sqlType
- the SQL type to resolve- Returns:
- the corresponding constant name in
Types
(e.g. "VARCHAR"/"NUMERIC"), ornull
if not resolvable - Since:
- 5.2
-
lookupColumnName
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 implementation takes into account recent 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
- the current meta-data to usecolumnIndex
- the index of the column for the lookup- Returns:
- the column name to use
- Throws:
SQLException
- in case of lookup failure
-
convertPropertyNameToUnderscoreName
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
- the property name to be converted- Returns:
- the column name using underscores
- Since:
- 6.1
- See Also:
-
convertUnderscoreNameToPropertyName
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
- the potentially underscores-based column name to be converted- Returns:
- the name using "camelCase"
- See Also:
-
extractDatabaseMetaData(DataSource, DatabaseMetaDataCallback)
with a lambda expression or method reference and a generically typed result