【问题标题】:Insert using PreparedStatement and return values of one column for inserted rows使用 PreparedStatement 插入并为插入的行返回一列的值
【发布时间】:2020-12-08 19:56:54
【问题描述】:

我正在使用PreparedStatement 批量插入 MySQL 数据库。然后我需要查询为成功插入的行返回一个特定列(名称)的值。我已经试过了:

preparedStatement = connection.prepareStatement("INSERT INTO x (id,name) VALUES (?,?)", new String[]{"name"});

但是,我还没有找到任何方法可以在preparedStatement 运行后获取它的返回值。我试过preparedStatemet.getGeneratedKeys();,但它只返回主键值。

【问题讨论】:

  • @SusanMustafa 谢谢。但它不是在谈论executeBatch,它指的是executeUpdate
  • executeBatchint[] 结果(带有 0 和 1)可用于从已使用名称的数组或列表中进行选择。
  • @JoopEggen 似乎是最好的解决方案。谢谢!

标签: java mysql jdbc prepared-statement


【解决方案1】:

使用生成的键查找插入的行。在该查询中返回您想要的任何列。

留在单个事务中以获得不变的结果。

【讨论】:

  • 谢谢,但它至少需要一个额外的查询!
猜你喜欢
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
相关资源
最近更新 更多