java - 如何在java中获取更新的行ID

标签 java mysql sql mongodb hibernate

    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?

提前致谢...

最佳答案

您可以使用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();
            }

关于java - 如何在java中获取更新的行ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001870/

相关文章:

Java:如何使用扫描仪检查文件是否包含整数或单词?

java - 如何使用socket.setSoTimeout()?

mysql - 如何检查子查询结果中是否存在ID?

php - 如何使用带有 PDO 的 PHP 计算 MySQL 表中行子集的大小(以字节为单位)?

php - SQL:如何找到重复值直到分离

java - 在spring security中使用自定义方法安全注解

android - 将android应用程序连接到xampp

sql - 从歌曲表中创建一个随机排序的播放列表并返回当前、下一首和上一首歌曲的名称?

mysql - 加快 MySQL SUM + JOINS 的速度

java - 是否可以通过 struts2 将压缩的对象发送到 JSP 页面?