2

In JDBCTemplate, I'm calling

public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
            return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
        }

with

getJdbcTemplate().query(usersByUsernameQuery, new String[] {username}, new RowMapper<UserDetails>()

My problem is I only have access to the usersByUsernameQuery argument, and I want to use the 'username' variable twice in it. For example:

usersByUsernameQuery = "Select * from ... ... where someColumn=? or anotherColumn=?" 

Since I'm only passing a string array with one element (username), how should I build my query?

renz
  • 1,072
  • 3
  • 11
  • 21

1 Answers1

7

You can pass the same argument twice in the jdbcTemplate..

getJdbcTemplate().query(usersByUsernameQuery, new String[] {username, username}, new RowMapper<UserDetails>()

This is completely valid and working.

Alternatively, you can go through the following thread if you can use a different variation of jdbcTemplate.query

How to execute IN() SQL queries with Spring's JDBCTemplate effectivly?

Community
  • 1
  • 1
Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35