Re: Information in SQLException
- From: "Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx>
- Date: 23 Nov 2005 23:59:16 +0100
"hombre" wrote...
> I am using JDBC to connect to a MySQL database. The database has a table
> 'customer' with a primary key that is automatically generated by my
> program. There is another unique key (customerID) which has to be entered
> by the user.
I don't understand why you've chosen to use more than one unique key, as it
sound to me as the latter would suffice...
> When the user chooes a value for customerID that is already in use, I get
> an SQLException (SQLState
> 23000, detailMessage = 'Duplicate entry xy for key z').
> 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 ? Is this string vendor independent ?
It's vendor dependent.
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/SQLException.html
If you don't have any *other* fields that can make that exact error, I guess
that you should be able to use the method "getErrorCode()" on the
SQLException.
I haven't MySQL installed at the moment, but it *should* give different
codes for different errors:
- 1022 for duplicates that is because of the primary key
- 1062 for duplicates that *isn't* the primary key
http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
I believe the more "vendor-independent"[1] SQLState (which you can retrieve
with "getSQLState()" ), isn't on that detailed level. SQLState 23000 only
means "Integrity constraint violation", which is the state for even more
errors that the two mentioned.
Try it, and tell us what happened... ;-)
// Bjorn A
[1] It's only enforced by those databases that conform to XOPEN conventions.
.
- Follow-Ups:
- Re: Information in SQLException
- From: hombre
- Re: Information in SQLException
- From: IchBin
- Re: Information in SQLException
- References:
- Information in SQLException
- From: hombre
- Information in SQLException
- Prev by Date: Information in SQLException
- Next by Date: Re: Information in SQLException
- Previous by thread: Information in SQLException
- Next by thread: Re: Information in SQLException
- Index(es):