0

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

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
JohnNewman
  • 29
  • 1
  • 10
  • 1
    Does this answer your question? [How to execute INSERT statement using JdbcTemplate class from Spring Framework](https://stackoverflow.com/questions/5026943/how-to-execute-insert-statement-using-jdbctemplate-class-from-spring-framework) – İsmail Y. Apr 12 '22 at 08:57

1 Answers1

0

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}
);
Rahul Gupta
  • 29
  • 1
  • 6