复制 Java 结果集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3817785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying Java ResultSet
提问by Peanut
I have a java.sql.ResultSetobject that I need to update. However the result set is not updatable. Unfortunately this is a constraint on the particular framework I'm using.
我有一个java.sql.ResultSet需要更新的对象。但是,结果集不可更新。不幸的是,这是对我正在使用的特定框架的限制。
What I'm trying to achieve here is taking data from a database, then manipulating a small amount of the data and finally the data is being written to a CSV file.
我在这里试图实现的是从数据库中获取数据,然后处理少量数据,最后将数据写入 CSV 文件。
At this stage I think my best option is to create a new result set object and copy the contents of the original result set into the new one, manipulating the data as I do so.
在这个阶段,我认为我最好的选择是创建一个新的结果集对象并将原始结果集的内容复制到新的结果集中,像我一样操作数据。
However, I've hunted high and low on Google and don't seem to be able to determine how to do this or whether it's even possible at all.
但是,我在谷歌上搜索了高低,似乎无法确定如何做到这一点,或者根本无法确定。
I'm new to everything Java so any assistance would be gratefully received.
我对 Java 的一切都很陌生,因此将不胜感激地收到任何帮助。
回答by Peanut
Thanks for the responses. In the end I found CachedRowSetwhich is exactly what I needed. With this I was able to disconnect the ResultSetobject and update it.
感谢您的回复。最后我发现CachedRowSet这正是我需要的。有了这个,我能够断开ResultSet对象并更新它。
What's more, because CachedRowSetimplements the ResultSetinterface I was still able to pass it to my file generation method which requires an object that implements ResultSet.
更重要的是,因为CachedRowSet实现了ResultSet接口,我仍然能够将它传递给我的文件生成方法,该方法需要一个实现 ResultSet 的对象。
回答by BalusC
The normal practice would be to map the ResultSetto a List<Entity>where Entityis your own class which contains information about the data represented by a single database row. E.g. User, Person, Address, Product, Order, etcetera, depending on what the table actually contains.
正常的做法是映射ResultSet到一个List<Entity>地方Entity是你自己的类,它包含关于由一个单一的数据库行表示的数据信息。例如User、Person、Address、Product、Order等,具体取决于表实际包含的内容。
List<Entity> entities = new ArrayList<Entity>();
// ...
while (resultSet.next()) {
Entity entity = new Entity();
entity.setId(resultSet.getLong("id"));
entity.setName(resultSet.getString("name"));
entity.setValue(resultSet.getInt("value"));
// ...
entities.add(entity);
}
// ...
return entities;
Then, you can access, traverse and modify it the usual Java way. Finally, when persisting it back in the DB, use a PreparedStatementto update them in batches in a single go.
然后,您可以按照通常的 Java 方式访问、遍历和修改它。最后,当将它持久化回数据库时,使用PreparedStatementa 一次性批量更新它们。
String sql = "UPDATE entity SET name = ?, value = ? WHERE id = ?";
// ...
statement = connection.prepareStatement(sql);
for (Entity entity : entities) {
statement.setString(1, entity.getName());
statement.setInt(2, entity.getValue());
statement.setLong(3, entity.getId());
// ...
statement.addBatch();
}
statement.executeBatch();
// ...
Note that some DB's have a limit on the batch size. Oracle's JDBC driver has a limit on around 1000 items. You may want to call executeBatch()every 1000 items then. It should be simple using a counter inside the loop.
请注意,某些数据库对批处理大小有限制。Oracle 的 JDBC 驱动程序限制了大约 1000 个项目。然后您可能希望executeBatch()每 1000 个项目调用一次。在循环内使用计数器应该很简单。

