Sunday, July 3, 2011

Inserting Rows

If the owner of the Coffee Break chain wants to add one or more coffees to what he offers, the owner will need to add one row to the COFFEES table for each new coffee, as is done in the following code fragment from JdbcRowSetSample. Notice that because the jdbcRs object is always connected to the database, inserting a row into a JdbcRowSet object is the same as inserting a row into a ResultSet object: You move to the cursor to the insert row, use the appropriate updater method to set a value for each column, and call the method insertRow:

      jdbcRs.moveToInsertRow();       jdbcRs.updateString("COF_NAME", "HouseBlend");       jdbcRs.updateInt("SUP_ID", 49);       jdbcRs.updateFloat("PRICE", 7.99f);       jdbcRs.updateInt("SALES", 0);       jdbcRs.updateInt("TOTAL", 0);       jdbcRs.insertRow();        jdbcRs.moveToInsertRow();       jdbcRs.updateString("COF_NAME", "HouseDecaf");       jdbcRs.updateInt("SUP_ID", 49);       jdbcRs.updateFloat("PRICE", 8.99f);       jdbcRs.updateInt("SALES", 0);       jdbcRs.updateInt("TOTAL", 0);       jdbcRs.insertRow(); 

When you call the method insertRow, the new row is inserted into the jdbcRs object and is also inserted into the database. The preceding code fragment goes through this process twice, so two new rows are inserted into the jdbcRs object and the database.

No comments:

Post a Comment