简体   繁体   中英

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

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}
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM