'jdbcTemplate query with 5 parameters for insert
I am building a Spring application and in my Repository I have to insert an sql query that has 5 parameters and I tried jdbcTemplate.query or queryForList or Map but it gives me error.
Here is the code:
@Repository
public class BilantErrRepository {
@Autowired
private DataSource dataSourceMail;
public List<BilantErr> search() {
List<BilantErr> info1 = new ArrayList<>();
BilantErr bilant = new BilantErr();
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSourceMail);
String sql = "INSERT into Aaa (cui,an, data_autorizare,operator,motivatie, tip_perioada) VALUES (?,?,sysdate,?,?,?)";
info1 = jdbcTemplate.query(sql, bilant.getCui(),bilant.getAn(),bilant.getOperator(),bilant.getOperator(),bilant.getPerioada());
return info1;
}
}
What method can I use to do this insert ? Thanks
Solution 1:[1]
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}
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul Gupta |
