【问题标题】:Apache DBUtils - Why need resultsethandler for Insert?Apache DBUtils - 为什么插入需要 resultsethandler?
【发布时间】:2015-12-04 16:00:07
【问题描述】:

我使用 Apache DBUtils 运行插入语句。但是,我不确定为什么我必须在这种情况下包含 ResultSetHandler:

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

insertbatch 的情况与此类似,它确实使用了 ResultSetHandler。我已经诉诸于使用batch 呼叫进行批量查询。谁能解释为什么我们需要insert 的结果集处理程序?

【问题讨论】:

    标签: sql batch-insert apache-commons-dbutils


    【解决方案1】:

    来自文档https://commons.apache.org/proper/commons-dbutils/apidocs/

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

    rsh - 用于从 ResultSet 创建结果对象的处理程序 自动生成的密钥。

    如果您在插入时生成id 的表中插入值,您可以将其取回,例如查看此答案如何手动执行此操作:https://stackoverflow.com/a/1915197/947111

    您需要ResultSetHandler&lt;T&gt; rsh 来迭代ResultSet,它返回了已创建的id

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 2016-01-20
      • 2011-06-16
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      相关资源
      最近更新 更多