Class JdbcTestUtils
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcountRowsInTable(JdbcOperations jdbcTemplate, String tableName) Count the rows in the given table.static intcountRowsInTable(JdbcClient jdbcClient, String tableName) Count the rows in the given table.static intcountRowsInTableWhere(JdbcOperations jdbcTemplate, String tableName, @Nullable String whereClause) Count the rows in the given table, using the providedWHEREclause.static intcountRowsInTableWhere(JdbcClient jdbcClient, String tableName, @Nullable String whereClause) Count the rows in the given table, using the providedWHEREclause.static intdeleteFromTables(JdbcOperations jdbcTemplate, String... tableNames) Delete all rows from the specified tables.static intdeleteFromTables(JdbcClient jdbcClient, String... tableNames) Delete all rows from the specified tables.static intdeleteFromTableWhere(JdbcOperations jdbcTemplate, String tableName, String whereClause, Object... args) Delete rows from the given table, using the providedWHEREclause.static intdeleteFromTableWhere(JdbcClient jdbcClient, String tableName, String whereClause, Object... args) Delete rows from the given table, using the providedWHEREclause.static voiddropTables(JdbcOperations jdbcTemplate, String... tableNames) Drop the specified tables.static voiddropTables(JdbcClient jdbcClient, String... tableNames) Drop the specified tables.
-
Constructor Details
-
JdbcTestUtils
public JdbcTestUtils()
-
-
Method Details
-
countRowsInTable
Count the rows in the given table.- Parameters:
jdbcTemplate- theJdbcOperationswith which to perform JDBC operationstableName- name of the table to count rows in- Returns:
- the number of rows in the table
-
countRowsInTable
Count the rows in the given table.- Parameters:
jdbcClient- theJdbcClientwith which to perform JDBC operationstableName- name of the table to count rows in- Returns:
- the number of rows in the table
- Since:
- 6.1
-
countRowsInTableWhere
public static int countRowsInTableWhere(JdbcOperations jdbcTemplate, String tableName, @Nullable String whereClause) Count the rows in the given table, using the providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedSELECTstatement. 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- theJdbcOperationswith which to perform JDBC operationstableName- the name of the table to count rows inwhereClause- theWHEREclause to append to the query- Returns:
- the number of rows in the table that match the provided
WHEREclause
-
countRowsInTableWhere
public static int countRowsInTableWhere(JdbcClient jdbcClient, String tableName, @Nullable String whereClause) Count the rows in the given table, using the providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedSELECTstatement. 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:
jdbcClient- theJdbcClientwith which to perform JDBC operationstableName- the name of the table to count rows inwhereClause- theWHEREclause to append to the query- Returns:
- the number of rows in the table that match the provided
WHEREclause - Since:
- 6.1
-
deleteFromTables
Delete all rows from the specified tables.- Parameters:
jdbcTemplate- theJdbcOperationswith which to perform JDBC operationstableNames- the names of the tables to delete from- Returns:
- the total number of rows deleted from all specified tables
-
deleteFromTables
Delete all rows from the specified tables.- Parameters:
jdbcClient- theJdbcClientwith which to perform JDBC operationstableNames- the names of the tables to delete from- Returns:
- the total number of rows deleted from all specified tables
- Since:
- 6.1
-
deleteFromTableWhere
public static int deleteFromTableWhere(JdbcOperations jdbcTemplate, String tableName, String whereClause, Object... args) Delete rows from the given table, using the providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedDELETEstatement. 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 theWHEREclause, binding to the given arguments.- Parameters:
jdbcTemplate- theJdbcOperationswith which to perform JDBC operationstableName- the name of the table to delete rows fromwhereClause- theWHEREclause to append to the queryargs- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also containSqlParameterValueobjects 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
-
deleteFromTableWhere
public static int deleteFromTableWhere(JdbcClient jdbcClient, String tableName, String whereClause, Object... args) Delete rows from the given table, using the providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedDELETEstatement. 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 theWHEREclause, binding to the given arguments.- Parameters:
jdbcClient- theJdbcClientwith which to perform JDBC operationstableName- the name of the table to delete rows fromwhereClause- theWHEREclause to append to the queryargs- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also containSqlParameterValueobjects 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
- Since:
- 6.1
-
dropTables
Drop the specified tables.- Parameters:
jdbcTemplate- theJdbcOperationswith which to perform JDBC operationstableNames- the names of the tables to drop
-
dropTables
Drop the specified tables.- Parameters:
jdbcClient- theJdbcClientwith which to perform JDBC operationstableNames- the names of the tables to drop- Since:
- 6.1
-