Something that I find crops up quite often is code to deal with duplicate keys. I often want to insert into a table, or update if the key already exists. In MySQL I can just use INSERT ... ON DUPLICATE KEY UPDATE ... for this, but with Derby I end up with code that looks like this:

  try {
    //... insert new row
  }
  catch (SQLException e) {
    if (e.getSQLState().equals(DUPLICATE_KEY)) {
      // ... update existing row
    }
    else {
      throw e;
    }
  }

In the absence of something like INSERT ... ON DUPLICATE KEY UPDATE, would it not perhaps be a good idea for Derby to subclass SQLException so that it could throw a (say) SQLKeyExistsException to avoid ugly repetitive code like the above? Or is there already something that I've overlooked that addresses this problem?

TIA,
--
John English

Reply via email to