snaq.db
Class SimpleQueryValidator

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

public class SimpleQueryValidator
extends QueryValidator

ConnectionValidator implementation that validates database connections by issuing a SQL query, and simply checking if the query succeeded or failed. By default it does not check if any results are returned.

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

Author:
Giles Winstanley

Constructor Summary
SimpleQueryValidator(String query)
          Creates a new SimpleQueryValidator instance using the specified SQL query string.
SimpleQueryValidator(String query, boolean checkHasResults)
          Creates a new SimpleQueryValidator instance using the specified SQL query string.
 
Method Summary
 boolean checkResults(ResultSet results)
          Checks the results of the SQL query to see if it indicates a valid connection.
 String getQueryString()
          Returns the SQL query string to be issued to the database.
 
Methods inherited from class snaq.db.QueryValidator
isValid
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleQueryValidator

public SimpleQueryValidator(String query)
Creates a new SimpleQueryValidator instance using the specified SQL query string.

Parameters:
query - SQL query to issue for validation

SimpleQueryValidator

public SimpleQueryValidator(String query,
                            boolean checkHasResults)
Creates a new SimpleQueryValidator instance using the specified SQL query string.

Parameters:
query - SQL query to issue for validation
checkHasResults - whether to check if any results are returned
Method Detail

getQueryString

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

Specified by:
getQueryString in class QueryValidator
Returns:
query string in SQL syntax

checkResults

public boolean checkResults(ResultSet results)
                     throws SQLException
Checks the results of the SQL query to see if it indicates a valid connection. This implementation simply comprises:
     return checkHasResults ? results.next() : true;
 
such that if results-checking was specified when the instance was created then the connection is only validated if some results are returned, otherwise the simply fact of the statement being executed successfully is used for validation.

Specified by:
checkResults in class QueryValidator
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