Reputation: 161
Need to insert records using spring jdbctemplate batch update. while inserting if duplicate record is found, the record needs to be updated else inserted. how do i do that? Below is my code.
Note:have not included exception handling.
result = jdbcTemplate.batchUpdate(
"insert ignore xxx set yy = ?, zz = ? where aa = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setDouble(1, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("aa").toString()));
ps.setDouble(2, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("bb").toString()));
ps.setString(3, new JSONObject(jsonArray.get(i).toString()).get("cc").toString());
}
public int getBatchSize() {
return jsonArray.length();
}
} );
}
Upvotes: 1
Views: 3405
Reputation: 539
You could use one of the following choices:
merge
sql command.Hope that helps
Upvotes: 1
Reputation: 2262
Need to insert records using spring jdbctemplate batch update. while inserting if duplicate record is found, the record needs to be updated else inserted
For this purpose you should use standard SQL-2003 command merge.
In MySQL this command has following syntax:
INSERT...ON DUPLICATE KEY UPDATE
Upvotes: 0