public class JdbcTestUtils extends Object
JdbcTestUtils
is a collection of JDBC related utility functions
intended to simplify standard database testing scenarios.JdbcTemplate
,
ScriptUtils
,
ResourceDatabasePopulator
,
DatabasePopulatorUtils
Constructor and Description |
---|
JdbcTestUtils() |
Modifier and Type | Method and 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 provided
WHERE 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 provided
WHERE clause. |
static void |
dropTables(JdbcTemplate jdbcTemplate,
String... tableNames)
Drop the specified tables.
|
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- name of the table to count rows inpublic static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)
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"
.
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- the name of the table to count rows inwhereClause
- the WHERE
clause to append to the queryWHERE
clausepublic static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableNames
- the names of the tables to delete frompublic static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)
WHERE
clause.
If the provided WHERE
clause contains text, it will be prefixed
with " WHERE "
and then appended to the generated DELETE
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 the WHERE
clause, binding to the given arguments.
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- the name of the table to delete rows fromwhereClause
- the WHERE
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 contain SqlParameterValue
objects which indicate not only the argument value but also the SQL type and
optionally the scale.public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableNames
- the names of the tables to drop