public abstract class JdbcUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
TYPE_UNKNOWN
Constant that indicates an unknown (or unspecified) SQL type.
|
Constructor and Description |
---|
JdbcUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
closeConnection(java.sql.Connection con)
Close the given JDBC Connection and ignore any thrown exception.
|
static void |
closeResultSet(java.sql.ResultSet rs)
Close the given JDBC ResultSet and ignore any thrown exception.
|
static void |
closeStatement(java.sql.Statement stmt)
Close the given JDBC Statement and ignore any thrown exception.
|
static java.lang.String |
commonDatabaseName(java.lang.String source)
Extract a common name for the target database in use even if
various drivers/platforms provide varying names at runtime.
|
static java.lang.String |
convertUnderscoreNameToPropertyName(java.lang.String name)
Convert a column name with underscores to the corresponding property name using "camel case".
|
static java.lang.Object |
extractDatabaseMetaData(javax.sql.DataSource dataSource,
DatabaseMetaDataCallback action)
Extract database meta-data via the given DatabaseMetaDataCallback.
|
static java.lang.Object |
extractDatabaseMetaData(javax.sql.DataSource dataSource,
java.lang.String metaDataMethodName)
Call the specified method on DatabaseMetaData for the given DataSource,
and extract the invocation result.
|
static java.lang.Object |
getResultSetValue(java.sql.ResultSet rs,
int index)
Retrieve a JDBC column value from a ResultSet, using the most appropriate
value type.
|
static java.lang.Object |
getResultSetValue(java.sql.ResultSet rs,
int index,
java.lang.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 java.lang.String |
lookupColumnName(java.sql.ResultSetMetaData resultSetMetaData,
int columnIndex)
Determine the column name to use.
|
static boolean |
supportsBatchUpdates(java.sql.Connection con)
Return whether the given JDBC driver supports JDBC 2.0 batch updates.
|
public static final int TYPE_UNKNOWN
Types
,
Constant Field Valuespublic static void closeConnection(java.sql.Connection con)
con
- the JDBC Connection to close (may be null
)public static void closeStatement(java.sql.Statement stmt)
stmt
- the JDBC Statement to close (may be null
)public static void closeResultSet(java.sql.ResultSet rs)
rs
- the JDBC ResultSet to close (may be null
)@UsesJava7 public static java.lang.Object getResultSetValue(java.sql.ResultSet rs, int index, java.lang.Class<?> requiredType) throws java.sql.SQLException
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.
rs
- is the ResultSet holding the dataindex
- is the column indexrequiredType
- the required value type (may be null
)java.sql.SQLException
- if thrown by the JDBC APIgetResultSetValue(ResultSet, int)
public static java.lang.Object getResultSetValue(java.sql.ResultSet rs, int index) throws java.sql.SQLException
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.
rs
- is the ResultSet holding the dataindex
- is the column indexjava.sql.SQLException
- if thrown by the JDBC APIBlob
,
Clob
,
Timestamp
public static java.lang.Object extractDatabaseMetaData(javax.sql.DataSource dataSource, DatabaseMetaDataCallback action) throws MetaDataAccessException
This method will open a connection to the database and retrieve the database meta-data. 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.
dataSource
- the DataSource to extract meta-data foraction
- callback that will do the actual workprocessMetaData
methodMetaDataAccessException
- if meta-data access failedpublic static java.lang.Object extractDatabaseMetaData(javax.sql.DataSource dataSource, java.lang.String metaDataMethodName) throws MetaDataAccessException
dataSource
- the DataSource to extract meta-data formetaDataMethodName
- the name of the DatabaseMetaData method to callMetaDataAccessException
- if we couldn't access the DatabaseMetaData
or failed to invoke the specified methodDatabaseMetaData
public static boolean supportsBatchUpdates(java.sql.Connection con)
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.
con
- the Connection to checkDatabaseMetaData.supportsBatchUpdates()
public static java.lang.String commonDatabaseName(java.lang.String source)
source
- the name as provided in database meta-datapublic static boolean isNumeric(int sqlType)
sqlType
- the SQL type to be checkedpublic static java.lang.String lookupColumnName(java.sql.ResultSetMetaData resultSetMetaData, int columnIndex) throws java.sql.SQLException
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.
resultSetMetaData
- the current meta-data to usecolumnIndex
- the index of the column for the look upjava.sql.SQLException
- in case of lookup failurepublic static java.lang.String convertUnderscoreNameToPropertyName(java.lang.String name)
name
- the column name to be converted