Detecting Duplicate Keys
Joe Sam Shirah adds: You can also get this information in a portable way, and potentially avoid another database access, by capturing SQLState messages. Some databases get more specific than others, but the general code portion is 23 - "Constraint Violations". UDB2, for example, gives a specific such as 23505, while others will only give 23000. For more information, see Where can I find a list of the possible SQLStates returned by SQLException.getSQLState()? and my JDBC 2.0 Fundamentals Short Course at the Java Developer Connection.