Hi All,
I am using Spring JDBCTemplate.batchUpdate() with PreparedStatement as shown below
Following is my Batch Setter classCode:String[][] myData = {{"param1", "value1"}, {"param2", "value2"}, {"param3", "value3"}}; String sql = "update myTable set param_value=? where id=? and param_name=?"; JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource()); MyUpdateBatchSetter setter = new MyUpdateBatchSetter(myData); int[] rowCounts = jdbcTemplate.batchUpdate(sql, setter);
Code:public class MyUpdateBatchSetter implements BatchPreparedStatementSetter { private String[][] data; public MyUpdateBatchSetter(String[][] data) { this.data = data; } // this is called for each row public void setValues( PreparedStatement ps, int i ) { System.out.println("data = " + data[i][1] + "\n" + data[i][0]); try { ps.setString( 1, data[i][1] ); // set first value ps.setInt( 2, 1000 ); // set second value ps.setString( 3, data[i][0] ); // set third value } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int getBatchSize() { return data.length; } }
This code is updating the database but the problem is that the number of affcted rows returned (rowCounts) is -2 for all the statements.
I need correct rowCounts for inserting the data if that was not updated as shown below
Code:for (int i = 0; i < rowCounts.length; i++) { if (rowCounts[i] <= 0) { // row wasn't updated we should insert it // Code for inserting goes here.... } }
It would be great if anybody can help.


Reply With Quote
.
