Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I had a working RowMapper code that maps database table row in to a java object. I wanted to change the implementation using lambda expression. However, I am always getting error; Code snippet as follows;

String queryString = "select * from person where person_id = ? ";
RowMapper rowMapper = (rs, rowNum) -> {
Person p = new Person();
p.setName(rs.getString("personName"));
p.setAddress(rs.getString("address"));
p.setAge(rs.getInt("age"));
Person person = getJdbcTemplate().query(queryString, personId, rowMapper);
return person;

Can someone help me to implement code correctly? Any idea to get list of persons?

What is the compilation error. Did you miss a semicolon after }?. Is RowMapper a functional interface? What version of spring are you using? – Chetan Kinger Jan 29, 2017 at 16:51 Yes, once I add semicolon compiler asked to add return statement inside the lambda. However, I need to return person object. – nwGCham Jan 29, 2017 at 17:03

RowMapper is a interface with a single abstract method (not inheriting from a method of Object), so it can be considered a functional interface. Its functional method takes a ResultSet and a int, and returns an object.

The first problem with the code is that the type of the object returned is a generic type of the interface. As currently used with RowMapper, you're using a raw-type, which you shouldn't do. The second issue is that the lambda expression does not return any object, so it cannot conform to the functional method which except an object to be returned.

As such, a corrected code would be:

RowMapper<Person> rowMapper = (rs, rowNum) -> {
    Person p = new Person();
    p.setName(rs.getString("personName"));
    p.setAddress(rs.getString("address"));
    p.setAge(rs.getInt("age"));
    return p;
                I got error when I compile the code.  311: error: ')' expected RowMapper<Person> rowMapper = (rs, rowNum) -> {  @Tunaki any idea with this...??
– nwGCham
                Jan 29, 2017 at 17:14
                Probably due to code around that @Gayath. Look at lines before. If you take the snippet in this answer alone, it compiles fine.
– Tunaki
                Jan 29, 2017 at 17:18

RowMapper using lambda expression example:

return jdbcTemplate.query(" select Scale_Point,Scale_Head from TEval_Scale ", new Object[] {},
                (resultSet, rowNum) ->{
                    TEvalScale tEvalScale = new TEvalScale();
                    tEvalScale.setScalePoint(resultSet.getInt("Scale_Point"));
                    tEvalScale.setScaleHead(resultSet.getString("Scale_Head"));
                    return tEvalScale;

@Tunaki is correct. Here is the shorthand version:

RowMapper<Person> rowMapper = (rs, rowNum) -> new Person(rs.getString("personName"), rs.getString("address"),rs.getInt("age")) ;

The brackets and the return aren't needed as they are implied.

RowMapper<Person> rowMapper = (rs, rowNum) -> {
    Person p = new Person();
    p.setName(rs.getString("personName"));
    p.setAddress(rs.getString("address"));
    p.setAge(rs.getInt("age"));
    return p;
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("person_id", personId);

If you want to get single Person :

Person person = getJdbcTemplate().queryForObject(queryString, paramMap, rowMapper);

If you want to get List of Person :

List<Person> personList = getJdbcTemplate().query(queryString, paramMap, rowMapper);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.