Bug #37326 How to find the MYSQL error codes in Java when db exception occurs
Submitted: 11 Jun 2008 3:05 Modified: 11 Jun 2008 4:28
Reporter: ravi paramasivam Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:MYSQL 4.11 OS:Any
Assigned to: CPU Architecture:Any
Tags: How to find the MYSQL error codes in Java when db exception occurs

[11 Jun 2008 3:05] ravi paramasivam
Description:
We have written a java code to perform some db operation(insert,delete,...) using mysql db.We have captured the general SQLException for all db errors.In that we wanted to differentiate the error messages based on the error codes for the following scenarios
  1.Duplicate Key Exception
  2.MYSQL Table is full,
  3.MYSQL Connection failure,
  4.MYSQL Syntax error,....
Qn:-
***
1.Where can we get the mysql error codes and error text mapping?
2.Will these error codes displayed in SQLException.getErrorCode() method?.If not how we can get it in Java code?

How to repeat:
1.Write a java code to insert some duplicate record
2.Catch the SQLException and log the error messages
[11 Jun 2008 3:28] Mark Matthews
> 1.Where can we get the mysql error codes and error text mapping?

See the constants in com.mysql.jdbc.MysqlErrorNumbers, or the MySQL server manual. For at least two of the three cases you mention above, if you're using Connector/J 5.1.x, you will get concrete SQLException classes, for example MySQLSyntaxErrorException and CommunicationsException.

> 2.Will these error codes displayed in SQLException.getErrorCode()

Yes.
[11 Jun 2008 4:28] ravi paramasivam
Thanks for the quick reply,We are using 
mysql version: 4.1.11-standard and
mysql-connector-java-3.0.16-ga-bin.jar so with this can we just take mysql-connector-java-5.1.x-rc-bin.jar and check for the exceptions?.Will it be backward compatible?
System is running commerically and hence we can't change the mysqversion as of now.But by giving the option we can change the connector/J jar file version.

Please suggest how to proceed?.

P.S
***
Can we have some reference link for the sample code?