【问题标题】:How to Get Updated row Id in java如何在java中获取更新的行ID
【发布时间】:2018-02-27 05:40:40
【问题描述】:
    Query branch_update = null;
    Branches branchObject = null;
    try {
        branch_update = this.sessionFactory.getCurrentSession()
                .createQuery("Update Branches set branchname =:branchname,update_date =:update_date WHERE branchname =:branchname1");
        branch_update.setParameter("branchname", stringCellValue);
        branch_update.setParameter("update_date", new Date());
        branch_update.setParameter("branchname1", stringCellValue);
        int update_id = branch_update.executeUpdate();
} catch (Exception exception) {
        logger.error("Exception occured at \t e", exception.getMessage(), exception);
    }

同时使用分支名称更新行。如何获取更新的行 branch_id ?

提前谢谢...

【问题讨论】:

  • 您是否使用 auto_increment 作为 branch_id?

标签: java mysql sql mongodb hibernate


【解决方案1】:

您可以使用getGeneratedKeys() method 获取结果集

 s.execute();  // perform the UPDATE
    try (ResultSet rs = s.getGeneratedKeys()) {
        while (rs.next()) {
            System.out.println(rs.getInt(1));  // print the "branch_id" value of the updated row
        }
    }

编辑:- 在那种情况下,我认为这应该会有所帮助

Iterator iterator = branch_update.list().iterator();
            while (iterator.hasNext()) {
                Branches branch = (Branches) iterator.next();
            }

【讨论】:

  • 我认为它不会在这里工作:它的目的是检索插入期间生成的密钥
  • 我正在使用 Hibernet sessionFactory "sessionFactory.getCurrentSession().createQuery("")" 我无法获取 getGeneratedKeys()..
猜你喜欢
  • 2011-06-28
  • 2021-10-11
  • 2010-11-26
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 2023-03-22
相关资源
最近更新 更多