0


I'm trying to save a Customer record here, using preparedstatementcreator.

public Customer saveRecord(Customer customer) {
        int result = 0;

        try {
            KeyHolder keyHolder = new GeneratedKeyHolder();
            result = jdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection connection)
                        throws SQLException {
                    PreparedStatement preparedStatement = connection.prepareStatement(SQL_INSERT_CUSTOMER_MASTER_WITH_AUTO_INCREMENT, Statement.RETURN_GENERATED_KEYS);
                    preparedStatement.setString(1, customer.getFirstname());
                    return preparedStatement;
                }
            }, keyHolder);
        } catch (DataAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result > 0 ? customer : null;
    }

And here is my Customer object.

public class Customer implements Serializable{

    private long id;
    private String firstname;
    private String secondname;
    private int age;
    private String address;
    private Country country;
    private String[] language;

    public Customer() {
    }

    public Customer(String firstname, String secondname, int age, String address, Country country, String[] language) {
        this.firstname = firstname;
        this.secondname = secondname;
        this.age = age;
        this.address = address;
        this.country = country;
        this.language = language;
    }

    //getters setters
}

I understand the reason here is we cannot access customer object from inside of createPreparedStatement().


So kind of modification I can do to original Customer object class to make it visible inside of this inner class?

Dil.
  • 1,996
  • 7
  • 41
  • 68
  • what is the exact error that you get ? – Damith Jan 08 '19 at 06:38
  • 1
    try marking your customer varibale as `final` when you want to access it in an inner class: `public Customer saveRecord(final Customer customer)` – STaefi Jan 08 '19 at 07:20
  • @staefi won't that cause any problems to the application process? – Dil. Jan 08 '19 at 08:49
  • @damith &Variable is accessed within inner class. Needs to be declared final – Dil. Jan 08 '19 at 08:52
  • @pippilongstocking: No it doesn't cause any problem. Any local variable which is going to be used in an inner/anonymous class should be final. – STaefi Jan 08 '19 at 09:19
  • @staefi my concern is this is a parameter. – Dil. Jan 08 '19 at 09:29
  • 1
    @pippilongstocking: by marking this parameter as final your are assuring that this variable (say parameter) reference is not going to be changed in the scope of your method. What problem would it cause that concerns you? – STaefi Jan 08 '19 at 09:57
  • @STaefi :D the problem is have never done that. :D Anyway it fixed the issue. Thanks a lot. – Dil. Jan 08 '19 at 10:02
  • 1
    @pippilongstocking: It's right, you never changed anything but compiler needs to be sure about that. Please read this post: https://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class . Glad that helped. – STaefi Jan 08 '19 at 10:05

0 Answers0