org.springframework.test.jdbc
Class JdbcTestUtils

java.lang.Object
  extended by org.springframework.test.jdbc.JdbcTestUtils

public class JdbcTestUtils
extends Object

JdbcTestUtils is a collection of JDBC related utility functions intended to simplify standard database testing scenarios.

As of Spring 3.1.3, JdbcTestUtils supersedes SimpleJdbcTestUtils.

Since:
2.5.4
Author:
Thomas Risberg, Sam Brannen, Juergen Hoeller

Constructor Summary
JdbcTestUtils()
           
 
Method Summary
static boolean containsSqlScriptDelimiters(String script, char delim)
          Determine if the provided SQL script contains the specified delimiter.
static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)
          Count the rows in the given table.
static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)
          Count the rows in the given table, using the provided WHERE clause.
static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)
          Delete all rows from the specified tables.
static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
          Drop the specified tables.
static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
          Execute the given SQL script.
static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError)
          Execute the given SQL script.
static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, String sqlResourcePath, boolean continueOnError)
          Execute the given SQL script.
static String readScript(LineNumberReader lineNumberReader)
          Read a script from the provided LineNumberReader, using "--" as the comment prefix, and build a String containing the lines.
static String readScript(LineNumberReader lineNumberReader, String commentPrefix)
          Read a script from the provided LineNumberReader, using the supplied comment prefix, and build a String containing the lines.
static void splitSqlScript(String script, char delim, List<String> statements)
          Split an SQL script into separate statements delimited with the provided delimiter character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcTestUtils

public JdbcTestUtils()
Method Detail

countRowsInTable

public static int countRowsInTable(JdbcTemplate jdbcTemplate,
                                   String tableName)
Count the rows in the given table.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
tableName - name of the table to count rows in
Returns:
the number of rows in the table

countRowsInTableWhere

public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate,
                                        String tableName,
                                        String whereClause)
Count the rows in the given table, using the provided WHERE clause.

If the provided WHERE clause contains text, it will be prefixed with " WHERE " and then appended to the generated SELECT statement. For example, if the provided table name is "person" and the provided where clause is "name = 'Bob' and age > 25", the resulting SQL statement to execute will be "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25".

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
tableName - the name of the table to count rows in
whereClause - the WHERE clause to append to the query
Returns:
the number of rows in the table that match the provided WHERE clause

deleteFromTables

public static int deleteFromTables(JdbcTemplate jdbcTemplate,
                                   String... tableNames)
Delete all rows from the specified tables.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
tableNames - the names of the tables to delete from
Returns:
the total number of rows deleted from all specified tables

dropTables

public static void dropTables(JdbcTemplate jdbcTemplate,
                              String... tableNames)
Drop the specified tables.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
tableNames - the names of the tables to drop

executeSqlScript

public static void executeSqlScript(JdbcTemplate jdbcTemplate,
                                    ResourceLoader resourceLoader,
                                    String sqlResourcePath,
                                    boolean continueOnError)
                             throws DataAccessException
Execute the given SQL script.

The script will typically be loaded from the classpath. There should be one statement per line. Any semicolons will be removed.

Do not use this method to execute DDL if you expect rollback.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
resourceLoader - the resource loader with which to load the SQL script
sqlResourcePath - the Spring resource path for the SQL script
continueOnError - whether or not to continue without throwing an exception in the event of an error
Throws:
DataAccessException - if there is an error executing a statement and continueOnError is false
See Also:
ResourceDatabasePopulator

executeSqlScript

public static void executeSqlScript(JdbcTemplate jdbcTemplate,
                                    Resource resource,
                                    boolean continueOnError)
                             throws DataAccessException
Execute the given SQL script.

The script will typically be loaded from the classpath. Statements should be delimited with a semicolon. If statements are not delimited with a semicolon then there should be one statement per line. Statements are allowed to span lines only if they are delimited with a semicolon.

Do not use this method to execute DDL if you expect rollback.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
resource - the resource to load the SQL script from
continueOnError - whether or not to continue without throwing an exception in the event of an error
Throws:
DataAccessException - if there is an error executing a statement and continueOnError is false
See Also:
ResourceDatabasePopulator

executeSqlScript

public static void executeSqlScript(JdbcTemplate jdbcTemplate,
                                    EncodedResource resource,
                                    boolean continueOnError)
                             throws DataAccessException
Execute the given SQL script.

The script will typically be loaded from the classpath. There should be one statement per line. Any semicolons will be removed.

Do not use this method to execute DDL if you expect rollback.

Parameters:
jdbcTemplate - the JdbcTemplate with which to perform JDBC operations
resource - the resource (potentially associated with a specific encoding) to load the SQL script from
continueOnError - whether or not to continue without throwing an exception in the event of an error
Throws:
DataAccessException - if there is an error executing a statement and continueOnError is false
See Also:
ResourceDatabasePopulator

readScript

public static String readScript(LineNumberReader lineNumberReader)
                         throws IOException
Read a script from the provided LineNumberReader, using "--" as the comment prefix, and build a String containing the lines.

Parameters:
lineNumberReader - the LineNumberReader containing the script to be processed
Returns:
a String containing the script lines
Throws:
IOException
See Also:
readScript(LineNumberReader, String)

readScript

public static String readScript(LineNumberReader lineNumberReader,
                                String commentPrefix)
                         throws IOException
Read a script from the provided LineNumberReader, using the supplied comment prefix, and build a String containing the lines.

Parameters:
lineNumberReader - the LineNumberReader containing the script to be processed
commentPrefix - the line prefix that identifies comments in the SQL script
Returns:
a String containing the script lines
Throws:
IOException

containsSqlScriptDelimiters

public static boolean containsSqlScriptDelimiters(String script,
                                                  char delim)
Determine if the provided SQL script contains the specified delimiter.

Parameters:
script - the SQL script
delim - character delimiting each statement — typically a ';' character
Returns:
true if the script contains the delimiter; false otherwise

splitSqlScript

public static void splitSqlScript(String script,
                                  char delim,
                                  List<String> statements)
Split an SQL script into separate statements delimited with the provided delimiter character. Each individual statement will be added to the provided List.

Parameters:
script - the SQL script
delim - character delimiting each statement — typically a ';' character
statements - the list that will contain the individual statements