Announcement Announcement Module
Collapse
No announcement yet.
Spring JdbcTemplate batchUpdate handling exceptions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring JdbcTemplate batchUpdate handling exceptions

    Currently I use batchUpdate method of JdbcTemplate to do batch Insertion.

    Problem :- In case if there is an error in one of the update, I need to just log it and continue with next update.

    The below code in case of exception stops when it encounters an exception.

    public void insertListOfPojos(final List<Student> myPojoList) {


    String sql = "INSERT INTO " + "Student " + "(name,age) "
    + "VALUES " + "(?,?)";

    jdbcTemplateObject.batchUpdate(sql,
    new BatchPreparedStatementSetter() {

    @Override
    public void setValues(PreparedStatement ps, int i)
    throws SQLException {

    Student myPojo = myPojoList.get(i);
    ps.setString(1, myPojo.getName());
    ps.setInt(2, myPojo.getAge());

    }

    @Override
    public int getBatchSize() {
    return myPojoList.size();
    }
    });

    }
Working...
X