Re: Information in SQLException
- From: hombre <hombre67@xxxxxx>
- Date: Thu, 24 Nov 2005 21:15:09 +0100
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 .
- Follow-Ups:
- Re: Information in SQLException
- From: IchBin
- Re: Information in SQLException
- References:
- Information in SQLException
- From: hombre
- Re: Information in SQLException
- From: Bjorn Abelli
- Re: Information in SQLException
- From: IchBin
- Re: Information in SQLException
- From: IchBin
- Re: Information in SQLException
- From: Bjorn Abelli
- Re: Information in SQLException
- From: IchBin
- Information in SQLException
- Prev by Date: Re: Information in SQLException
- Next by Date: Re: Joining every row from 1 table to every row from another
- Previous by thread: Re: Information in SQLException
- Next by thread: Re: Information in SQLException
- Index(es):