0

I run an insert statement using Apache DBUtils. However, I am not sure why I have to include ResultSetHandler for this case:

String theQuery = QueryGenerator.insertintoStats();
        ResultSetHandler<Object> dummyHandler = new ResultSetHandler<Object>() {
            @Override
            public Object handle(ResultSet rs) throws SQLException
            {
                return null;
            }
        };
        try
        {
            queryRunner.insert(connection, theQuery, dummyHandler, Constants.UUIDSTR.toString(), name, prevbackupTime,
                    curbackupTime, updStartTime, delStartTime, bkupType.toString(), rowCount);
        }
        catch (SQLException e)
        {
            LOGGER.info(theQuery.toString());
            LOGGER.error("Caught exception!", e);
        }

Similar's the case for insertbatch which does use ResultSetHandler. I have resorted to use batch call for batch queries. Can anyone explain why we would be needing resultset handler for insert?

dmachop
  • 824
  • 1
  • 21
  • 39

1 Answers1

1

From documentation https://commons.apache.org/proper/commons-dbutils/apidocs/:

public <T> T insert(String sql,
           ResultSetHandler<T> rsh,
           Object... params)
         throws SQLException

rsh - The handler used to create the result object from the ResultSet of auto-generated keys.

If you insert values in a table which generate id upon insertion, you can retrieve it back, for example see this answer how to do this manually : https://stackoverflow.com/a/1915197/947111

You need ResultSetHandler<T> rsh to iterate over ResultSet which returned with id's which has been created.

Community
  • 1
  • 1
Anatoly
  • 5,056
  • 9
  • 62
  • 136