JDBCUtilpublic abstract class JDBCUtil extends Object Helper class for managing common JDBC tasks.
This class is abstract to allow implementations to
take advantage of different logging capabilities/interfaces in
different parts of the code. |
Methods Summary |
---|
public void | closeJDBCConnection(java.sql.Connection conn)Closes database connection and logs if an error
is encountered
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
// Log exception and continue
subclassLogWrapper("Unexpected exception while closing database connection.");
}
| public void | closeJDBCResultSet(java.sql.ResultSet aResultSet)Closes database result set and logs if an error
is encountered
try {
if (aResultSet != null) {
aResultSet.close();
}
} catch (SQLException sqle) {
// Log exception and continue
subclassLogWrapper("Unexpected exception while closing database result set.");
}
| public void | closeJDBCStatement(java.sql.Statement stmt)Closes database statement and logs if an error
is encountered
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException sqle) {
// Log exception and continue
subclassLogWrapper("Unexpected exception while closing database statement.");
}
| public boolean | columnExists(java.sql.DatabaseMetaData dbMetaData, java.lang.String tableName, java.lang.String columnName)Checks database metadata to see if a column exists in a table
Try UPPER, lower, and MixedCase, both on the table name and the column name, to see if the column is there.
return ( columnExistsCaseSensitive(dbMetaData, tableName, columnName) ||
columnExistsCaseSensitive(dbMetaData, tableName, columnName.toUpperCase(Locale.US)) ||
columnExistsCaseSensitive(dbMetaData, tableName, columnName.toLowerCase(Locale.US)) ||
columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName) ||
columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName.toLowerCase(Locale.US)) ||
columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName) ||
columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName.toLowerCase(Locale.US)) );
| public boolean | columnExistsCaseSensitive(java.sql.DatabaseMetaData dbMetaData, java.lang.String tableName, java.lang.String columnName)Checks database metadata to see if a column exists in a table. This method
is sensitive to the case of both the provided table name and column name.
ResultSet rsTables = dbMetaData.getColumns(null, null, tableName, columnName);
try {
boolean found = rsTables.next();
return found;
} finally {
closeJDBCResultSet(rsTables);
}
| protected abstract void | delegatedLog(java.lang.String errorString)An abstract method which child classes override to handle logging of
errors in their particular environments.
| private void | subclassLogWrapper(java.lang.String logString)Wraps the delegated call to the subclass logging method with a Throwable
wrapper. All throwables generated by the subclass logging method are
caught and ignored.
try {
delegatedLog(logString);
}
catch(Throwable t) {
// Throwables generated by the logging system are ignored
}
| public boolean | tableExists(java.sql.DatabaseMetaData dbMetaData, java.lang.String tableName)Checks database metadata to see if a table exists.
Try UPPER, lower, and MixedCase, to see if the table is there.
return ( tableExistsCaseSensitive(dbMetaData, tableName) ||
tableExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US)) ||
tableExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US)) );
| public boolean | tableExistsCaseSensitive(java.sql.DatabaseMetaData dbMetaData, java.lang.String tableName)Checks database metadata to see if a table exists. This method
is sensitive to the case of the provided table name.
ResultSet rsTables = dbMetaData.getTables(null, null, tableName, null);
try {
boolean found = rsTables.next();
return found;
} finally {
closeJDBCResultSet(rsTables);
}
|
|