mar*_*ark 6 java postgresql jdbc batch-file
这是有效的代码:
Connection c = ds.getConnection();
c.setAutoCommit(false);
PreparedStatement stmt = c.prepareStatement("INSERT INTO items (name, description) VALUES(?, ?)");
while (!(items = bus.take()).isEmpty()) {
for (Item item : items) {
stmt.setString(1, item.name);
stmt.setString(2, item.description);
stmt.addBatch();
}
stmt.executeBatch();
c.commit();
}
Run Code Online (Sandbox Code Playgroud)
但现在我需要填充另一个表,其中id是外键.如果我使用INSERT RETURNING id然后executeBatch失败并且"当没有预期时返回结果"错误.
我看到了解决这个问题的几种方法
在我看到的三种方法中,最后一种方法似乎既保留了批量插入的效率又返回了id,但对我来说这也是最复杂的,因为我从未编写过存储过程.
有没有更好的方法批量插入和获取ID?我使用postgresql特定的API而不是jdbc没有问题.
如果没有,任何人都可以草拟这样的存储过程吗?
这是表模式:
CREATE UNLOGGED TABLE items
(
id serial,
name character varying(1000),
description character varying(10000)
)
WITH (
OIDS=FALSE
);
Run Code Online (Sandbox Code Playgroud)
a_h*_*ame 10
这样的事情应该有效:
// tell the driver you want the generated keys
stmt = c.prepareStatement("INSERT ... ", Statement.RETURN_GENERATED_KEYS);
stmt.executeBatch();
// now retrieve the generated keys
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next()) {
int id = rs.getInt(1);
.. save the id somewhere or update the items list
}
Run Code Online (Sandbox Code Playgroud)
我认为(我不肯定!),该密钥在生成它们的顺序返回.因此,ResultSet中的第一行应映射到您正在处理的列表中的第一个"项".但要确认一下!
编辑
如果这不起作用,请尝试指定为其生成值的实际列:
stmt = c.prepareStatement("INSERT ... ", new String[] {"id"});
Run Code Online (Sandbox Code Playgroud)