org.afcs.warts.util
Class DatabaseUtils

java.lang.Object
  extended byorg.afcs.warts.util.DatabaseUtils

public final class DatabaseUtils
extends java.lang.Object

The DatabaseUtils class provides numerous utility methods that simplify database use. The most useful methods simply wrap standard library methods providing error handling and recovery functionality that simplify the calling code.

LICENSE: This code is released to the public domain and may be used for any purpose whatsoever without permission or acknowledgment.

Version:
Last Modified 18 September 2003
Author:
Warren Hedley ( whedley at sdsc dot edu )

Method Summary
static boolean closeConnection(java.sql.Connection connection)
          Calls close() on the Connection object received, if non-null.
static boolean closeResultSet(java.sql.ResultSet resultSet)
          Ensures that the ResultSet object received is closed, if non-null.
static boolean closeStatement(java.sql.Statement statement)
          Ensures that the Statement object received is closed, if non-null.
static java.lang.String escapeTextForSqlQuery(java.lang.String inputString)
          This method should be used to pre-process character data that is to be used in straight SQL statements.
static void handleSQLException(java.sql.SQLException sqlException)
          This method can be used by any database access classes to handle SQL exceptions in a consistent manner.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

escapeTextForSqlQuery

public static java.lang.String escapeTextForSqlQuery(java.lang.String inputString)
This method should be used to pre-process character data that is to be used in straight SQL statements. It method looks for any occurrences of special characters (particularly apostrophes) in the string that could cause problems when inserted into the query, and attempts to escape them in such a way that they become harmless. This allows, for instance, authors to create comments that contain apostrophes. This method should only be used for string data.

Note that this method should not be used when using PreparedStatements, as the JDBC driver has its own escaping mechanism.

Parameters:
inputString - The string to be verified.
Returns:
The input string with all special characters escaped so that this can be used in a SQL query.

closeConnection

public static boolean closeConnection(java.sql.Connection connection)
Calls close() on the Connection object received, if non-null. Use of this method makes database access code more concise and easier to read, by removing exception handling code.

Parameters:
connection - The connection to call rollback() on.
Returns:
True if the operation seemed successful.

closeResultSet

public static boolean closeResultSet(java.sql.ResultSet resultSet)
Ensures that the ResultSet object received is closed, if non-null. Use of this method makes database access code more concise and easier to read, by removing the not-null check and exception handling code.

Parameters:
resultSet - The result set to close.
Returns:
True if the operation seemed successful.

closeStatement

public static boolean closeStatement(java.sql.Statement statement)
Ensures that the Statement object received is closed, if non-null. Use of this method makes database access code more concise and easier to read, by removing the not-null check and exception handling code.

Parameters:
statement - The statement to close.
Returns:
True if the operation seemed successful.

handleSQLException

public static void handleSQLException(java.sql.SQLException sqlException)
This method can be used by any database access classes to handle SQL exceptions in a consistent manner. Currently the default behaviour is simply to print a stacktrace to the command line. In the medium term, we'll need a better logging facility.

Parameters:
sqlException - The error to handle.