Java Reference
In-Depth Information
An application task has queried the database and obtained results. You have stored
those results into a ResultSet object, and you want to update some of those values
in the ResultSet and commit them back to the database.
Solution
Make your ResultSet object updatable, and then update the rows as needed while
iterating through the results. The following example method demonstrates how to make
ResultSet updatable and then how to update content within that ResultSet ,
eventually persisting it in the database:
private static void queryAndUpdateDbRecipes(String
recipeNumber){
String sql = "SELECT ID, RECIPE_NUMBER,
RECIPE_NAME, DESCRIPTION " +
"FROM RECIPES " +
"WHERE RECIPE_NUMBER = ?";
ResultSet rs = null;
try (PreparedStatement pstmt =
conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_
UPDATABLE);){
pstmt.setString(1, recipeNumber);
rs = pstmt.executeQuery();
while(rs.next()){
String desc = rs.getString(4);
System.out.println("Updating row" + desc);
rs.updateString(4, desc + " -- More to
come");
rs.updateRow();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (rs != null){
Search WWH ::




Custom Search