com.googlecode.flyway.core.util.jdbc
Class JdbcTemplate

java.lang.Object
  extended by com.googlecode.flyway.core.util.jdbc.JdbcTemplate
Direct Known Subclasses:
DB2JdbcTemplate, DerbyJdbcTemplate, H2JdbcTemplate, HsqlJdbcTemplate, MySQLJdbcTemplate, OracleJdbcTemplate, PostgreSQLJdbcTemplate, SQLServerJdbcTemplate

public abstract class JdbcTemplate
extends java.lang.Object

Collection of utility methods for querying the DB. Inspired by Spring's JdbcTemplate.


Constructor Summary
JdbcTemplate(java.sql.Connection connection)
          Creates a new JdbcTemplate.
 
Method Summary
 void execute(java.lang.String sql, java.lang.Object... params)
          Executes this sql statement using a PreparedStatement.
 void executeStatement(java.lang.String sql)
          Executes this sql statement using an ordinary Statement.
 java.sql.Connection getConnection()
           
 java.sql.DatabaseMetaData getMetaData()
          Retrieves the database metadata for the connection associated with this JdbcTemplate.
 boolean hasTables(java.lang.String catalog, java.lang.String schema, java.lang.String table, java.lang.String... tableTypes)
          Checks whether the database contains tables matching these criteria.
<T> java.util.List<T>
query(java.lang.String query, RowMapper<T> rowMapper)
          Executes this query and map the results using this row mapper.
 int queryForInt(java.lang.String query, java.lang.String... params)
          Executes this query with these parameters against this connection.
 java.util.List<java.util.Map<java.lang.String,java.lang.String>> queryForList(java.lang.String query, java.lang.String... params)
          Executes this query with these parameters against this connection.
 java.lang.String queryForString(java.lang.String query, java.lang.String... params)
          Executes this query with these parameters against this connection.
 java.util.List<java.lang.String> queryForStringList(java.lang.String query, java.lang.String... params)
          Executes this query with these parameters against this connection.
protected abstract  void setNull(java.sql.PreparedStatement preparedStatement, int parameterIndex)
          Sets the value of the parameter with this index to null in this PreparedStatement.
 void update(java.lang.String sql, java.lang.Object... params)
          Executes this update sql statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcTemplate

public JdbcTemplate(java.sql.Connection connection)
Creates a new JdbcTemplate.

Parameters:
connection - The DB connection to use.
Method Detail

getConnection

public java.sql.Connection getConnection()
Returns:
The DB connection to use.

queryForList

public java.util.List<java.util.Map<java.lang.String,java.lang.String>> queryForList(java.lang.String query,
                                                                                     java.lang.String... params)
                                                                              throws java.sql.SQLException
Executes this query with these parameters against this connection.

Parameters:
query - The query to execute.
params - The query parameters.
Returns:
The query results.
Throws:
java.sql.SQLException - when the query execution failed.

queryForStringList

public java.util.List<java.lang.String> queryForStringList(java.lang.String query,
                                                           java.lang.String... params)
                                                    throws java.sql.SQLException
Executes this query with these parameters against this connection.

Parameters:
query - The query to execute.
params - The query parameters.
Returns:
The query results as a list of strings.
Throws:
java.sql.SQLException - when the query execution failed.

queryForInt

public int queryForInt(java.lang.String query,
                       java.lang.String... params)
                throws java.sql.SQLException
Executes this query with these parameters against this connection.

Parameters:
query - The query to execute.
params - The query parameters.
Returns:
The query result.
Throws:
java.sql.SQLException - when the query execution failed.

queryForString

public java.lang.String queryForString(java.lang.String query,
                                       java.lang.String... params)
                                throws java.sql.SQLException
Executes this query with these parameters against this connection.

Parameters:
query - The query to execute.
params - The query parameters.
Returns:
The query result.
Throws:
java.sql.SQLException - when the query execution failed.

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Retrieves the database metadata for the connection associated with this JdbcTemplate.

Returns:
The database metadata.
Throws:
java.sql.SQLException - when the database metadata could not be retrieved.

hasTables

public boolean hasTables(java.lang.String catalog,
                         java.lang.String schema,
                         java.lang.String table,
                         java.lang.String... tableTypes)
                  throws java.sql.SQLException
Checks whether the database contains tables matching these criteria.

Parameters:
catalog - The catalog where the table resides. (optional)
schema - The schema where the table resides. (optional)
table - The name of the table. (optional)
tableTypes - The types of table to look for (ex.: TABLE). (optional)
Returns:
true if matching tables have been found, false if not.
Throws:
java.sql.SQLException - when the check failed.

execute

public void execute(java.lang.String sql,
                    java.lang.Object... params)
             throws java.sql.SQLException
Executes this sql statement using a PreparedStatement.

Parameters:
sql - The statement to execute.
params - The statement parameters.
Throws:
java.sql.SQLException - when the execution failed.

executeStatement

public void executeStatement(java.lang.String sql)
                      throws java.sql.SQLException
Executes this sql statement using an ordinary Statement.

Parameters:
sql - The statement to execute.
Throws:
java.sql.SQLException - when the execution failed.

update

public void update(java.lang.String sql,
                   java.lang.Object... params)
            throws java.sql.SQLException
Executes this update sql statement.

Parameters:
sql - The statement to execute.
params - The statement parameters.
Throws:
java.sql.SQLException - when the execution failed.

setNull

protected abstract void setNull(java.sql.PreparedStatement preparedStatement,
                                int parameterIndex)
                         throws java.sql.SQLException
Sets the value of the parameter with this index to null in this PreparedStatement.

Parameters:
preparedStatement - The prepared statement whose parameter to set.
parameterIndex - The index of the parameter to set.
Throws:
java.sql.SQLException - when the value could not be set.

query

public <T> java.util.List<T> query(java.lang.String query,
                                   RowMapper<T> rowMapper)
                        throws java.sql.SQLException
Executes this query and map the results using this row mapper.

Type Parameters:
T - The type of the result objects.
Parameters:
query - The query to execute.
rowMapper - The row mapper to use.
Returns:
The list of results.
Throws:
java.sql.SQLException - when the query failed to execute.


Copyright © 2012. All Rights Reserved.