Hint
This article is generated by AI translation.
Template Methods
dbVisitor supports the following template method usage patterns:
Get a Connection
ConnectionCallback gives you direct access to java.sql.Connection. JdbcTemplate acquires/releases the connection and handles exceptions.
T resultList = jdbcTemplate.execute((ConnectionCallback<T>) con -> {
return ...;
});
Get a Statement
StatementCallback exposes java.sql.Statement. JdbcTemplate manages connection acquisition, statement creation/closure, and exception handling.
T resultList = jdbcTemplate.execute((StatementCallback<T>) stmt -> {
return ...;
});
Custom PreparedStatement Setup
Template methods let you customize PreparedStatement creation.
- Set arguments, read results via ResultSetExtractor
T result = jdbc.executeCreator(con -> {
PreparedStatement ps = con.prepareStatement("select * from users where id > ?");
ps.setString(1, "Bob");
return ps;
}, (ResultSetExtractor) rs -> {
return ...;
}); - Set arguments, read results via RowMapper
List<User> result = jdbc.executeCreator(con -> {
PreparedStatement ps = con.prepareStatement("select * from users where id > ?");
ps.setString(1, "Bob");
return ps;
}, (RowMapper<User>) (rs, rowNum) -> {
return ...;
}); - Set arguments, process rows via RowCallbackHandler
jdbc.executeCreator(con -> {
PreparedStatement ps = con.prepareStatement("select * from users where id > ?");
ps.setString(1, "Bob");
return ps;
}, (RowCallbackHandler) (rs, rowNum) -> {
// process one row
});