org.springframework.jdbc.support
Class JdbcUtils

java.lang.Object
  extended byorg.springframework.jdbc.support.JdbcUtils

public class JdbcUtils
extends Object

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, Juergen Hoeller

Constructor Summary
JdbcUtils()
           
 
Method Summary
static void closeConnection(Connection con)
          Close the given JDBC Connection and ignore any thrown exception.
static void closeResultSet(ResultSet rs)
          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 int countParameterPlaceholders(String str, char placeholder, char delim)
          Count the occurrences of the character placeholder in an SQL string str.
static int countParameterPlaceholders(String str, char placeholder, String delimiters)
          Count the occurrences of the character placeholder in an SQL string str.
static Object extractDatabaseMetaData(DataSource dataSource, DatabaseMetaDataCallback action)
          Extract database meta data via the given DatabaseMetaDataCallback.
static Object extractDatabaseMetaData(DataSource dataSource, String metaDataMethodName)
          Call the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.
static boolean isNumeric(int sqlType)
          Check that a SQL type is numeric.
static boolean supportsBatchUpdates(Connection con)
          Return whether the given JDBC driver supports JDBC 2.0 batch updates.
static int translateType(int sqlType)
          Translate a SQL type into one of a few values: All integer types are translated to Integer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcUtils

public JdbcUtils()
Method Detail

closeConnection

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 - the JDBC Connection to close

closeStatement

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 - the JDBC Statement to close

closeResultSet

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 - the JDBC ResultSet to close

extractDatabaseMetaData

public static Object extractDatabaseMetaData(DataSource dataSource,
                                             DatabaseMetaDataCallback action)
                                      throws MetaDataAccessException
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.

Parameters:
dataSource - the DataSource to extract metadata for
action - 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

extractDatabaseMetaData

public static Object extractDatabaseMetaData(DataSource dataSource,
                                             String metaDataMethodName)
                                      throws MetaDataAccessException
Call the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.

Parameters:
dataSource - the DataSource to extract meta data for
metaDataMethodName - 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:
DatabaseMetaData

supportsBatchUpdates

public static boolean supportsBatchUpdates(Connection con)
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:
DatabaseMetaData.supportsBatchUpdates()

countParameterPlaceholders

public static int countParameterPlaceholders(String str,
                                             char placeholder,
                                             char delim)
Count the occurrences of the character placeholder in an SQL string str. The character placeholder is not counted if it appears within a literal as determined by the delim that is passed in. Delegates to the overloaded method that takes a String with multiple delimiters.

Parameters:
str - string to search in. Returns 0 if this is null
placeholder - the character to search for and count.
delim - the delimiter for character literals.

countParameterPlaceholders

public static int countParameterPlaceholders(String str,
                                             char placeholder,
                                             String delimiters)
Count the occurrences of the character placeholder in an SQL string str. The character placeholder is not counted if it appears within a literal as determined by the delimiters that are passed in.

Examples: If one of the delimiters is the single quote, and the character to count the occurrences of is the question mark, then:

The big ? 'bad wolf?' gives a count of one.
The big ?? bad wolf gives a count of two.
The big 'ba''ad?' ? wolf gives a count of one.

The grammar of the string passed in should obey the rules of the JDBC spec which is close to no rules at all: one placeholder per parameter, and it should be valid SQL for the target database.

Parameters:
str - string to search in. Returns 0 if this is null
placeholder - the character to search for and count.
delimiters - the delimiters for character literals.

isNumeric

public static boolean isNumeric(int sqlType)
Check that a SQL type is numeric.

Parameters:
sqlType - the SQL type to be checked
Returns:
if the type is numeric

translateType

public static int translateType(int sqlType)
Translate a SQL type into one of a few values: All integer types are translated to Integer. All real types are translated to Double. All string types are translated to String. All other types are left untouched.

Parameters:
sqlType - the type to be translated into a simpler type
Returns:
the new SQL type


Copyright (C) 2003-2004 The Spring Framework Project.