Declaring a RowMapper with a lambda expression -- Wrong number of parameters: Expected 1 but got 2

215 Views Asked by At

I'm learning Spring and I was following along with this tutorial, but suddenly I started getting an error around this RowMapper assignment.

RowMapper<Person> rowMapper = (resultSet, i) -> {
           UUID id = UUID.fromString(resultSet.getString("id"));
           String name = resultSet.getString("name");
           return new Person(id, name);
       };

It's telling me that I'm using the wrong number of parameters in the lambda expression. My code is identical to the video. I've looked up several other similar questions on StackOverflow and they all line up with what I have. What am I doing wrong?

1

There are 1 best solutions below

0
On

Hey there so i was also following the video and found this to be the problem

package com.example.demo.dao;

import com.example.demo.model.Person;
//import org.flywaydb.core.internal.jdbc.JdbcTemplate; <- this is the wrong import
import org.springframework.jdbc.core.JdbcTemplate; //<- use this import (on the second line when choosing to auto import)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository("postgres")
public class PersonDataAccessService implements PersonDao{

    private final JdbcTemplate jdbcTemplate;   //the import for this 

and the main code in the video will work with out a hitch.

@Override
    public List<Object> selectAllPeople() {
        try {
            final String sql = "SELECT id, name FROM person";
            return jdbcTemplate.query(sql, (resultSet, i) -> {
                UUID id = UUID.fromString(resultSet.getString("id"));
                String name = resultSet.getString("name");
                return new Person(id, name);
            });
        } catch (Exception e) {
            // Handle the exception appropriately (e.g., logging, error message, etc.)
            throw new RuntimeException("Error occurred while retrieving people from the database", e);
        }
    }