Mobile ResultSet
Posted by Mike Haller
on Saturday, February 10. 2007
at 23:25
Once in a while, you want to have your data on the client. Then, let the client fiddle with the data and write it back into the database.You can do that using a Service layer, sure. That's the good way to do it. However, what if you don't want to have the overhead. Probably because you have a huge amount of tables. And you just want to have a very quick way to load and save data into them. So you can start creating screens and forms immediately.

Have a look at the RowSet stuff which is already in Java since ages. There is a CachedRowSet which can be serialized to the client. On the client side, the CachedRowSet can be used to execute SQL statments and update the data. Then, serialize it back to the server and apply the changes. Simple, isn't it?
Let's have a look on how to do that:
// On the Server: Populate some data
final CachedRowSet set1 = new CachedRowSetImpl();
set1.setCommand("SELECT id,name FROM currency");
set1.execute(newConnection());
byte[] serialized1 = serialize(set1);
// On the Client: Modify the data, add a new currency!
final CachedRowSet set2 = deserialize(serialized1);
set2.moveToInsertRow();
set2.updateInt("id", 3);
set2.updateString("name", "Dollar");
set2.insertRow();
byte[] serialized2 = serialize(set2);
// On the Server: store the new currency
assertEquals(2, countAmountOfCurrenciesOnServer());
final CachedRowSet set3 = deserialize(serialized2);
set3.moveToCurrentRow();
set3.acceptChanges(newConnection());
assertEquals(3, countAmountOfCurrenciesOnServer());
Note the set3.moveToCurrentRow(), it's mandatory. If you omit it, you will receive NullPointerException in the Sun implementation of CachedRowSet.
