How to execute INSERT statement using JdbcTemplate class from Spring Framework

user469999 picture user469999 · Feb 17, 2011 · Viewed 118.2k times · Source

In Spring, how can I insert data in table using JdbcTemplate. Can anyone please provide me a code sample for doing this.

Answer

user11153 picture user11153 · May 27, 2014

Use jdbcTemplate.update(String sql, Object... args) method:

jdbcTemplate.update(
    "INSERT INTO schema.tableName (column1, column2) VALUES (?, ?)",
    var1, var2
);

or jdbcTemplate.update(String sql, Object[] args, int[] argTypes), if you need to map arguments to SQL types manually:

jdbcTemplate.update(
    "INSERT INTO schema.tableName (column1, column2) VALUES (?, ?)",
    new Object[]{var1, var2}, new Object[]{Types.TYPE_OF_VAR1, Types.TYPE_OF_VAR2}
);