By jfbrown on Jan 25, 2006
I've noticed a few confusing forum posts on just what the heck our CachedRowSet is capable of.
CachedRowSet implements ResultSet. The ResultSet javadoc describes itself as " A table of data representing a database result set, which is usually generated by executing a statement that queries the database. " (the emphasis is mine.)
It works by first getting the "rows" of data from the database. This is done by executing a database query, i.e. an sql SELECT statement. Note: stored procedures can also return a ResultSet, but we do not currently support stored procs.
You cannot set the command of a CachedRowSet to an sql UPDATE or DELETE statement. These statements do not return a ResultSet when executed.
After you have a populated CachedRowSet (again, because you've executed some SELECT statement), you can then update or delete rows that exists within the CachedRowSet, or you can insert entirely new rows. To propagate your changes to the database, call acceptChanges(). You can also do the update or delete of rows through the DataProvider interface. For more info on doing this, see the tutorials for Accessing Databases on the Java Studio Creator Tutorials page.
But what if you want to batch update many rows, let's say reduce every manager's salary by 10%:
update EMP_TBL set salary=salary\*.90 where title='manager'
This single UPDATE statement is efficient - it's all done within the database server process.
However, to do this with a CachedRowSet is not efficient. First, you'd have to compose a select statement that included the primary key and the column(s) to be updated. You execute it. All rows to be updated get transferred to the web server where your code resides. Now your code has to loop through each row, setting the new salary value. Lastly, you acceptChanges(). If you look at the logging for what happens during the acceptChanges(), you'll see we do one SELECT and one UPDATE for each row in the CachedRowSet that has changed.
For a twenty row update, the single UPDATE statement sends one single command to the db server. The CachedRowSet solution does the initial select, creates a bunch of java objects, then does 20 SELECT and 20 UPDATE commands to the db server.
So how do you that single UPDATE within Creator? Use plain old JDBC.
For the next release, we hope to provide a framework or at least helper classes to assist you. But for now, you're on your own.
One final note: our CachedRowSet is implemented with class CachedRowSetXImpl (which implements our own CachedRowSetX). The eXtension of jdbc's CachedRowSet adds the advanced properties you see in the CachedRowSet's property sheet; most of these properties are used to work around problems in database driver metadata, or allow fine tuning of the generated INSERT and UPDATE statements.