ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Making the Most of JDBC with WebRowSet
Pages: 1, 2, 3, 4, 5

Manipulating ResultSet

You can delete a row by moving cursor to the row position and calling the deleteRow() method on the WebRowSet instance. Similarly, you can update one or more values in a row by moving the cursor to the row position and calling the appropriate update method. The following code example does both.



... ... ...
ResultSet rs = 
  stmt.executeQuery("select * from student ");
WebRowSet wrs = new WebRowSetImpl();
wrs.populate(rs);
wrs.absolute(1); //move to first row
wrs.deleteRow(); //delete current row
wrs.absolute(2); //move to second row
wrs.updateInt(4,20);//update current row
... ... ...

The first four lines of the code snippet are obvious: instantiate the object and populate it with the result set. The next two lines try to delete the first row. The final two lines try to update the value at fourth column of second row to 20. Outputting this WebRowSet object produces the following <data> element.

 <data>
   <deleteRow> 
     <columnValue>200</columnValue> 
     <columnValue>Jack</columnValue> 
     <columnValue>Dakota</columnValue> 
     <columnValue>21</columnValue> 
   </deleteRow> 
   <currentRow> 
     <columnValue>100</columnValue> 
     <columnValue>John</columnValue> 
     <columnValue>Doe</columnValue> 
     <columnValue>26</columnValue> 
     <updateRow>20</updateRow> 
   </currentRow> 
 </data>

This XML snippet shows that the first row is marked for deletion with the <deleteRow> tag. The value in the fourth column position of the second row is marked for update from current value of 26 to a new value of 20.

Committing the Changes

So far I've only manipulated the result. These changes will not be reflected in the underlying database until I commit. The following code, if executes successfully, makes the changes permanent by committing them.

... ... ...
wrs.acceptChanges();
... ... ...

Pages: 1, 2, 3, 4, 5

Next Pagearrow