Class JdbcTestUtils
- java.lang.Object
- org.springframework.test.jdbc.JdbcTestUtils
public abstract class JdbcTestUtils extends Object
JdbcTestUtils
is a collection of JDBC related utility functions intended to simplify standard database testing scenarios.- Since:
- 2.5.4
- Author:
- Thomas Risberg, Sam Brannen, Juergen Hoeller, Phillip Webb, Chris Baldwin
- See Also:
JdbcTemplate
,ScriptUtils
,ResourceDatabasePopulator
,DatabasePopulatorUtils
Constructor Summary
Constructors Constructor Description JdbcTestUtils()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 providedWHERE
clause.static int
deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)
Delete all rows from the specified tables.static int
deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)
Delete rows from the given table, using the providedWHERE
clause.static void
dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
Drop the specified tables.
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 operationstableName
- 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 providedWHERE
clause.If the provided
WHERE
clause contains text, it will be prefixed with" WHERE "
and then appended to the generatedSELECT
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 operationstableName
- the name of the table to count rows inwhereClause
- theWHERE
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 operationstableNames
- the names of the tables to delete from- Returns:
- the total number of rows deleted from all specified tables
deleteFromTableWhere
public static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)
Delete rows from the given table, using the providedWHERE
clause.If the provided
WHERE
clause contains text, it will be prefixed with" WHERE "
and then appended to the generatedDELETE
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"DELETE FROM person WHERE name = 'Bob' and age > 25"
.As an alternative to hard-coded values, the
"?"
placeholder can be used within theWHERE
clause, binding to the given arguments.- Parameters:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- the name of the table to delete rows fromwhereClause
- theWHERE
clause to append to the queryargs
- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also containSqlParameterValue
objects which indicate not only the argument value but also the SQL type and optionally the scale.- Returns:
- the number of rows deleted from the table
dropTables
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
Drop the specified tables.- Parameters:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableNames
- the names of the tables to drop