org.springframework.jdbc.support
Class JdbcUtils


public class JdbcUtils

Utility methods for SQL statements.

Author:
Isabelle Muszynski, Thomas Risberg, Juergen Hoeller
Version: $Id: JdbcUtils.java,v 1.4 2004/01/29 23:22:49 jhoeller Exp $

Method Summary
 static voidcloseResultSet(ResultSet rs)
          Close the given JDBC ResultSet and ignore any thrown exception.
 static voidcloseStatement(Statement stmt)
          Close the given JDBC Statement and ignore any thrown exception.
 static intcountParameterPlaceholders(String str, char placeholder, char delim)
          Count the occurrences of the character placeholder in an SQL string str.
 static booleanisNumeric(int sqlType)
          Check that a SQL type is numeric
 static inttranslateType(int sqlType)
          Translate a SQL type into one of a few values.

Method Detail

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

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

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.

Examples : if the delimiter 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.
delim - the delimiter 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: true if the type is numeric, false otherwise

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