sampad chakraborty
sampad chakraborty

Reputation: 9

How to pass several parameters as a list into a JDBC template

this is an example:

public int addEmplyee(int id) {
    return jdbcTemplate.update(
      "INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA");
}

If instead of passing id, "Bill", "Gates" and "USA" as a parameter, I want to store them in a list and want to pass it that way, how can I do it?

Upvotes: 0

Views: 980

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

update() takes a vararg (Object...) as argument. This is just syntactic sugar for an array. So you can just pass an array as argument:

return jdbcTemplate.update(
  "INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", new Object[] {id, "Bill", "Gates", "USA"});

If you jave a List, then simply transform it to an array.

Upvotes: 1

Related Questions