private int encryptColumnContents(String columnName,String tableName) throws Exception {
  int recordsEncrypted=0;
  String idColumnName=tableName + "_ID";
  StringBuffer selectSql=new StringBuffer();
  selectSql.append("SELECT " + idColumnName + ","+ columnName);
  selectSql.append(" FROM " + tableName);
  selectSql.append(" ORDER BY " + idColumnName);
  StringBuffer updateSql=new StringBuffer();
  updateSql.append("UPDATE " + tableName);
  updateSql.append(" SET " + columnName + "=?");
  updateSql.append(" WHERE " + idColumnName + "=?");
  PreparedStatement selectStmt=null;
  PreparedStatement updateStmt=null;
  selectStmt=m_conn.prepareStatement(selectSql.toString(),ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
  updateStmt=m_conn.prepareStatement(updateSql.toString());
  ResultSet rs=selectStmt.executeQuery();
  for (recordsEncrypted=0; rs.next(); ++recordsEncrypted) {
    int id=rs.getInt(1);
    String value=rs.getString(2);
    value=SecureEngine.encrypt(value);
    updateStmt.setString(1,value);
    updateStmt.setInt(2,id);
    if (updateStmt.executeUpdate() != 1) {
      log.warning("EncryptError: Table=" + tableName + ", ID="+ id);
      throw new Exception();
    }
  }
  rs.close();
  selectStmt.close();
  updateStmt.close();
  return recordsEncrypted;
}

private int encryptColumnContents ( String columnName , String tableName ) throws Exception { int recordsEncrypted = 0 ; String idColumnName = tableName + "_ID" ; StringBuffer selectSql = new StringBuffer ( ) ; selectSql . append ( "SELECT " + idColumnName + "," + columnName ) ; selectSql . append ( " FROM " + tableName ) ; selectSql . append ( " ORDER BY " + idColumnName ) ; StringBuffer updateSql = new StringBuffer ( ) ; updateSql . append ( "UPDATE " + tableName ) ; updateSql . append ( " SET " + columnName + "=?" ) ; updateSql . append ( " WHERE " + idColumnName + "=?" ) ; PreparedStatement selectStmt = null ; PreparedStatement updateStmt = null ; selectStmt = m_conn . prepareStatement ( selectSql . toString ( ) , ResultSet . TYPE_FORWARD_ONLY , ResultSet . CONCUR_UPDATABLE ) ; updateStmt = m_conn . prepareStatement ( updateSql . toString ( ) ) ; ResultSet rs = selectStmt . executeQuery ( ) ; for ( recordsEncrypted = 0 ; rs . next ( ) ; ++ recordsEncrypted ) { int id = rs . getInt ( 1 ) ; String value = rs . getString ( 2 ) ; value = SecureEngine . encrypt ( value ) ; updateStmt . setString ( 1 , value ) ; updateStmt . setInt ( 2 , id ) ; if ( updateStmt . executeUpdate ( ) != 1 ) { log . warning ( "EncryptError: Table=" + tableName + ", ID=" + id ) ; throw new Exception ( ) ; } } rs . close ( ) ; selectStmt . close ( ) ; updateStmt . close ( ) ; return recordsEncrypted ; }
encrypt all the contents of a database column .
