![](/img/trans.png)
[英]SQLException when using PreparedStatement.RETURN_GENERATED_KEYS
[英]PreparedStatement.RETURN_GENERATED_KEYS not working with batch insert
帶有返回生成鍵的預准備語句為單個插入返回自動生成的鍵,但不適用於批量插入。 有沒有辦法在插入過程中獲取自動生成的值。
PreparedStatement ps = connection.prepareStatement("insert into table() values()",PreparedStatement.RETURN_GENERATED_KEYS);
{
ps.addBatch();
}
ps.executeBatch();
ResultSet rs = ps.getGeneratedKeys();
List<Long> ids = new ArrayList<Long>();
while(rs.next()) {
ids.add(rs.getLong(1));
}
有沒有辦法在插入過程中獲取自動生成的值。
不。在ps.executeBatch()
您需要調用ps.getGeneratedKeys()
並循環遍歷它返回的 ResultSet。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.