Java JDBC ResultSet create updatable result set

Description

Java JDBC ResultSet create updatable result set

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
   //from   www .  j  a  v a2s  .  com
   public static void queryAndUpdateDbRecipes(Connection conn){
      String sql = "SELECT ID, RECIPE_NUM, NAME, DESCRIPTION " +
                   "FROM RECIPES " +
                   "WHERE RECIPE_NUM = ?";
      PreparedStatement pstmt = null;
      try {
          pstmt = conn.prepareStatement(sql, 
                     ResultSet.TYPE_SCROLL_SENSITIVE, 
                     ResultSet.CONCUR_UPDATABLE);
          
          pstmt.setString(1, "1001");
          ResultSet 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 (pstmt != null){
              try {
                  pstmt.close();
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
          }
      }
      
  }
}



PreviousNext

Related