snaq.db
Class QueryValidator

java.lang.Object
  extended by snaq.db.QueryValidator
All Implemented Interfaces:
ConnectionValidator
Direct Known Subclasses:
SimpleQueryValidator

public abstract class QueryValidator
extends Object
implements ConnectionValidator

Abstract ConnectionValidator implementation that validates database connections by issuing a SQL query.

To create a simple ConnectionValidator implementation which validates using a SQL query, extend this class and implement the abstract methods.

For example, the getQueryString() method could be implemented to return the string:

SELECT * FROM test WHERE test_id < 10;

This would cause this SQL query to be issued to the database whenever a connection needs validating. If the query causes a SQLException to be thrown then the validation automatically fails. If the query completes successfully, the generated ResultSet is then passed to the checkResults(ResultSet) method.

This class is provided as a convenience for providing connection validation.

Author:
Giles Winstanley

Constructor Summary
QueryValidator()
           
 
Method Summary
abstract  boolean checkResults(ResultSet results)
          Checks the results of the SQL query to see if it indicates a valid connection.
abstract  String getQueryString()
          Returns the SQL query string to be issued to the database.
 boolean isValid(Connection con)
          Determines whether the specified connection is good to use.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryValidator

public QueryValidator()
Method Detail

isValid

public final boolean isValid(Connection con)
                      throws SQLException
Determines whether the specified connection is good to use.

Specified by:
isValid in interface ConnectionValidator
Parameters:
con - Connection instance to check for validity
Returns:
true if the specified connection is good to use, false otherwise
Throws:
SQLException

getQueryString

public abstract String getQueryString()
Returns the SQL query string to be issued to the database.

Returns:
query string in SQL syntax

checkResults

public abstract boolean checkResults(ResultSet results)
                              throws SQLException
Checks the results of the SQL query to see if it indicates a valid connection.

Parameters:
results - ResultSet instance produced from SQL query
Returns:
boolean indicating whether checked results indicated a valid connection
Throws:
SQLException - if checking the results throws such an exception