|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
An update is the modification of a column value in the current row. Let's suppose that we want to raise the price of French Roast Decaf coffee to 10.99. Using the JDBC 1.0 API, the update would look something like this:stmt.executeUpdate("UPDATE COFFEES SET PRICE = 10.99" + "WHERE COF_NAME = FRENCH_ROAST_DECAF");The following code fragment shows another way to accomplish the update, this time using the JDBC 2.0 API:
uprs.last(); uprs.updateFloat("PRICE", 10.99);Update operations in the JDBC 2.0 API affect column values in the row where the cursor is positioned, so in the first line the
ResultSetuprscalls the methodlastto move its cursor to the last row (the row where the columnCOF_NAMEhas the valueFRENCH_ROAST_DECAF). Once the cursor is on the last row, all of the update methods you call will operate on that row until you move the cursor to another row. The second line changes the value in thePRICEcolumn to 10.99 by calling the methodupdateFloat. This method is used because the column value we want to update is afloatin the Java programming language.The
ResultSet.updateXXXmethods take two parameters: the column to update and the new value to put in that column. As with theResultSet.getXXXmethods, the parameter designating the column may be either the column name or the column number. There is a differentupdateXXXmethod for updating each datatype (updateString,updateBigDecimal,updateInt, and so on) just as there are differentgetXXXmethods for retrieving different datatypes.At this point, the price in
uprsfor French Roast Decaf will be 10.99, but the price in the tableCOFFEESin the database will still be 9.99. To make the update take effect in the database and not just the result set, we must call theResultSetmethodupdateRow. Here is what the code should look like to update bothuprsandCOFFEES:uprs.last(); uprs.updateFloat("PRICE", 10.99f); uprs.updateRow();If you had moved the cursor to a different row before calling the method
updateRow, the update would have been lost. If, on the other hand, you realized that the price should really have been 10.79 instead of 10.99, you could have cancelled the update to 10.99 by calling the methodcancelRowUpdates. You have to invokecancelRowUpdatesbefore invoking the methodupdateRow; onceupdateRowis called, calling the methodcancelRowUpdatesdoes nothing. Note thatcancelRowUpdatescancels all of the updates in a row, so if there are many invocations of theupdateXXXmethods on the same row, you cannot cancel just one of them. The following code fragment first cancels updating the price to 10.99 and then updates it to 10.79:uprs.last(); uprs.updateFloat("PRICE", 10.99); uprs.cancelRowUpdates(); uprs.updateFloat("PRICE", 10.79); uprs.updateRow();In this example, only one column value was updated, but you can call an appropriate
updateXXXmethod for any or all of the column values in a single row. The concept to remember is that updates and related operations apply to the row where the cursor is positioned. Even if there are many calls toupdateXXXmethods, it takes only one call to the methodupdateRowto update the database with all of the changes made in the current row.If you want to update the price for
COLOMBIAN_DECAFas well, you have to move the cursor to the row containing that coffee. Because the row forCOLOMBIAN_DECAFimmediately precedes the row forFRENCH_ROAST_DECAF, you can call the methodpreviousto position the cursor on the row forCOLOMBIAN_DECAF. The following code fragment changes the price in that row to 9.79 in both the result set and the underlying table in the database:uprs.previous(); uprs.updateFloat("PRICE", 9.79); uprs.updateRow();All cursor movements refer to rows in a
ResultSetobject, not rows in the underlying database. If a query selects five rows from a database table, there will be five rows in the result set, with the first row being row 1, the second row being row 2, and so on. Row 1 can also be identified as the first, and, in a result set with five rows, row 5 is the last.The ordering of the rows in the result set has nothing at all to do with the order of the rows in the base table. In fact, the order of the rows in a database table is indeterminate. The DBMS keeps track of which rows were selected, and it makes updates to the proper rows, but they may be located anywhere in the table. When a row is inserted, for example, there is no way to know where in the table it has been inserted.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.