Re: Information in SQLException



hombre wrote:
IchBin schrieb:
Bjorn Abelli wrote:

IchBin wrote:

Bjorn Abelli wrote:

"hombre" wrote...


How do I know that the SLQException is thrown
because of the duplicate customerID and not because
of a duplicate primary key or any other sort of SQL
error. Do I have to parse the detailMessage-String ?


[on getErrorCode]

 - 1022 for duplicates that is because of the primary key
 - 1062 for duplicates that *isn't* the primary key


Try some like this.. Could use either state or code checks

try {
      ..
      ..
      int updatedRowCount = sqlStatement.executeUpdate(sqlCmd);

      } catch (SQLException ex) {
       // handle any errors

        if  (ex.getSQLState()) == 23000 | ex.getErrorCode() == 1022){
             dialogToDisplayYourMessage();
       }

Sorry.. if (ex.getSQLState()) == 23000 && ex.getErrorCode() == 1022){



SQLState 23000 also catches what the OP didn't want to catch, duplicate primary keys.


ErrorCode 1022 definitely catches what the OP didn't want to catch, duplicate primary keys.

It should suffice with:

   if (ex.getErrorCode() == 1062)

But that will still only work with MySQL, and if the table doesn't have any more unique fields than the primary key and CustomerID.

// Bjorn A


Sorry Bjorn, I need to slow down before I reply..

The following lines catch what I want:
catch( SQLException e ) {
    if (e.getSQLState() == "23000" && e.getErrorCode() == 1062)

But I am not happy with this solution because I have to make code modifications when I switch to another database. I wonder why there is a SQL99 or XOPEN standard that can't be used in practice.

Thanks,
Hombre

Well the standard applies to the language and not vendor generated messages based on there DBMS implementation. As a DBMS vendor they can have different feature that they need to add their own nomenclatures of messages. This will never be standardized. At least it has not been in my 28 years of computer field. If you have worked with Oracle and than most any other database you would understand.


There is a way to fix your error message problem. Have an external errorcode.properties file that you can map\change to what you want based on a different database. This way you do not have to change code. On you DB.open perform method\function to fetch these changeable error codes. But then you could just change the If statement for the hard-coded codes.


--


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________


'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
.