Class JdbcTemplate
- java.lang.Object
-
- migratedb.v1.core.api.internal.jdbc.JdbcTemplate
-
- Direct Known Subclasses:
BigQueryJdbcTemplate,SpannerJdbcTemplate
public class JdbcTemplate extends Object
Collection of utility methods for querying the DB. Inspired by Spring's JdbcTemplate.
-
-
Field Summary
Fields Modifier and Type Field Description protected ConnectionconnectionThe DB connection to use.protected intnullTypeThe type to assign to a null value.
-
Constructor Summary
Constructors Constructor Description JdbcTemplate(Connection connection, DatabaseType databaseType)Creates a new JdbcTemplate.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute(String sql, Object... params)Executes this sql statement using a PreparedStatement.ResultsexecuteStatement(String sql)Executes this sql statement using an ordinary Statement.voidextractErrors(Results results, SQLException e)ConnectiongetConnection()protected PreparedStatementprepareStatement(String sql, Object[] params)Creates a new prepared statement for this sql with these params.<T> List<T>query(String sql, RowMapper<T> rowMapper, Object... params)Executes this query and map the results using this row mapper.booleanqueryForBoolean(String query, String... params)Executes this query with these parameters against this connection.intqueryForInt(String query, String... params)Executes this query with these parameters against this connection.List<Map<String,String>>queryForList(String query, Object... params)Executes this query with these parameters against this connection.StringqueryForString(String query, String... params)Executes this query with these parameters against this connection.List<String>queryForStringList(String query, String... params)Executes this query with these parameters against this connection.voidupdate(String sql, Object... params)Executes this update sql statement.
-
-
-
Field Detail
-
connection
protected final Connection connection
The DB connection to use.
-
nullType
protected final int nullType
The type to assign to a null value.
-
-
Constructor Detail
-
JdbcTemplate
public JdbcTemplate(Connection connection, DatabaseType databaseType)
Creates a new JdbcTemplate.- Parameters:
connection- The database connection to use.
-
-
Method Detail
-
getConnection
public Connection getConnection()
- Returns:
- The DB connection to use.
-
queryForList
public List<Map<String,String>> queryForList(String query, Object... params) throws 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:
SQLException- when the query execution failed.
-
queryForStringList
public List<String> queryForStringList(String query, String... params) throws 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:
SQLException- when the query execution failed.
-
queryForInt
public int queryForInt(String query, String... params) throws 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:
SQLException- when the query execution failed.
-
queryForBoolean
public boolean queryForBoolean(String query, String... params) throws 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:
SQLException- when the query execution failed.
-
queryForString
public String queryForString(String query, String... params) throws 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:
SQLException- when the query execution failed.
-
execute
public void execute(String sql, Object... params) throws SQLException
Executes this sql statement using a PreparedStatement.- Parameters:
sql- The statement to execute.params- The statement parameters.- Throws:
SQLException- when the execution failed.
-
executeStatement
public Results executeStatement(String sql)
Executes this sql statement using an ordinary Statement.- Parameters:
sql- The statement to execute.- Returns:
- the results of the execution.
-
extractErrors
public void extractErrors(Results results, SQLException e)
-
update
public void update(String sql, Object... params) throws SQLException
Executes this update sql statement.- Parameters:
sql- The statement to execute.params- The statement parameters.- Throws:
SQLException- when the execution failed.
-
prepareStatement
protected PreparedStatement prepareStatement(String sql, Object[] params) throws SQLException
Creates a new prepared statement for this sql with these params.- Parameters:
sql- The sql to execute.params- The params.- Returns:
- The new prepared statement.
- Throws:
SQLException- when the statement could not be prepared.
-
query
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... params) throws SQLException
Executes this query and map the results using this row mapper.- Type Parameters:
T- The type of the result objects.- Parameters:
sql- The query to execute.rowMapper- The row mapper to use.- Returns:
- The list of results.
- Throws:
SQLException- when the query failed to execute.
-
-