It would be common practice to map ResultSet to List<Entity> , where Entity is your own class that contains information about the data represented by a single row of the database. For example. User , Person , Address , Product , Order , etc., depending on what the table actually contains.
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, move and modify it in the usual way of Java. Finally, when saving it to the database, use PreparedStatement to update them in packages at a time.
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(); // ...
Please note that some databases have a limit on the lot size. The Oracle JDBC driver has a limit of about 1000 elements. You can call executeBatch() every 1000 points. It should be simple using a counter inside the loop.
See also:
Balusc
source share