spring-framework / org.springframework.jdbc.support / JdbcUtils

JdbcUtils

abstract class JdbcUtils

Generic utility methods for working with JDBC. Mainly for internal use within the framework, but also useful for custom JDBC access code.

Author
Thomas Risberg

Author
Juergen Hoeller

Constructors

<init>

JdbcUtils()

Generic utility methods for working with JDBC. Mainly for internal use within the framework, but also useful for custom JDBC access code.

Properties

TYPE_UNKNOWN

static val TYPE_UNKNOWN: Int

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

Functions

closeConnection

open static fun closeConnection(con: Connection): Unit

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

closeResultSet

open static fun closeResultSet(rs: ResultSet): Unit

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

closeStatement

open static fun closeStatement(stmt: Statement): Unit

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

commonDatabaseName

open static fun commonDatabaseName(source: String): String

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

convertUnderscoreNameToPropertyName

open static fun convertUnderscoreNameToPropertyName(name: String): String

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

extractDatabaseMetaData

open static fun extractDatabaseMetaData(dataSource: DataSource, action: DatabaseMetaDataCallback): Any

Extract database meta data via the given DatabaseMetaDataCallback.

This method will open a connection to the database and retrieve the database metadata. Since this method is called before the exception translation feature is configured for a datasource, this method can not rely on the SQLException translation functionality.

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.

open static fun <T : Any> extractDatabaseMetaData(dataSource: DataSource, metaDataMethodName: String): T

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

getResultSetValue

open static fun getResultSetValue(rs: ResultSet, index: Int, requiredType: Class<*>): Any

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.

open static fun getResultSetValue(rs: ResultSet, index: Int): Any

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.

isNumeric

open static fun isNumeric(sqlType: Int): Boolean

Check whether the given SQL type is numeric.

lookupColumnName

open static fun lookupColumnName(resultSetMetaData: ResultSetMetaData, columnIndex: Int): String

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.

supportsBatchUpdates

open static fun supportsBatchUpdates(con: Connection): Boolean

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.