Next, you learn to update rows in a result set, using methods in the Java programming language rather than having to send an SQL command.
Updating a row in a
ResultSetobject is a two-phase process. First, the new value for each column being updated is set, and then the change is applied to the row. The row in the underlying data source is not updated until the second phase is completed.The
ResultSetinterface contains two update methods for each JDBC™ type, one specifying the column to be updated as an index and one specifying the column name as it appears in the select list. Column names supplied to updater methods are case insensitive. If a select list contains the same column more than once, the first instance of the column will be updated.First, you need to create a
ResultSetobject that is updatable. To do this, supply theResultSetconstantCONCUR_UPDATABLEto thecreateStatementmethod, as you have seen in previous examples. TheStatementobject it creates produces an updatableResultSetobject each time it executes a query. The following code fragment illustrates creating the updatableResultSetobjectuprs. Note that the code also makesuprsscrollable. An updatableResultSetobject does not necessarily have to be scrollable, but when you are making changes to a result set, you generally want to be able to move around in it. With a scrollable result set, you can move to rows you want to change, and if the type isTYPE_SCROLL_SENSITIVE, you can get the new value in a row after you have changed it.
Connection con = DriverManager.getConnection("jdbc:mySubprotocol:mySubName"); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");The
ResultSetobjectuprslooks something like this:COF_NAME PRICE ------------------ ----- Colombian 7.99 French_Roast 8.99 Espresso 9.99 Colombian_Decaf 8.99 French_Roast_Decaf 9.99The method
updateRowapplies all column changes to the current row. The changes are not made to the row untilupdateRowhas been called. You can use thecancelUpdatesmethod to back out changes made to the row before theupdateRowmethod is called.Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); ResultSet srs = stmt.executeQuery("select COF_Name from COFFEES " + "where price = 7.99"); srs.next(); srs.updateString("COF_NAME", "Foldgers"); srs.updateRow();An update is the modification of a column value in the current row. Let's suppose that you want to raise the price of French Roast Decaf coffee to 10.99:
uprs.last(); uprs.updateFloat("PRICE", 10.99);Update operations 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 to10.99by 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.