Movie Repository

Java Spring Database abstraction Public New



Consider the following Spring bean:

public class MovieRepository {

    private DataSource dataSource;

    public List<Movie> findMoviesByName(String name) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate.query("select name, year, rating from movies where name like ?",
                new Object[]{name},
                new RowMapper<Movie>() {
                    public Movie mapRow(ResultSet resultSet, int i) throws SQLException {
                        return new Movie(resultSet.getString("name"),
                                resultSet.getInt("year"), resultSet.getInt("rating"));


Select all of the statements that are correct.

The jdbcTemplate will open a new connection to the database every time the code is executed.
The name parameter being passed unescaped to JdbcTemplate is a potential security problem.
As JdbcTemplate is thread-safe there is no need to create a new instance every time findMoviesByName() is called.
To inject a DataSource, the class needs to be annotated with @Repository.
A BeanPropertyRowMapper could be used to automatically map database rows to instances of the Movie class, provided the Movie class has the appropriate setter methods.