File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Mysql Error Messages in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Mysql Error Messages in Java" Watch "Mysql Error Messages in Java" New topic

Mysql Error Messages in Java

Jose Dobles

Joined: Feb 17, 2011
Posts: 6
Hello. I'm finishing a java app that works with a mysql database. I need a way to capture mysql errors and display them with a customized text. For example, if I try to delete a row that has a foreign key to another table, I need to catch that error, and display a JOptionPane.showMessageDialog saying something like "Cannot delete the row. Please make sure the ID has no other links". Same with when I get the duplicity error on primary keys. Any ideas?
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

It may be that I have misunderstood your question, but if your app is using JDBC to update the database, then any database errors will cause SQLExceptions to be thrown.
Indeed, since SQLException is a checked exception, your java code must handle them.

Good Luck,
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

SQLExceptions always included the message the database server generated. You'll need to parse the message and watch for specific patterns that denote your constraint violation errors and report when one appears.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

SQLException also has a method called getErrorCode() that you can use to retrieve the error code. This error code is of course driver specific, but you can look up the possible values on the MySQL site (somewhere) and perform your own mapping from error codes to error messages.

I did a little searching and found this URL. For instance,

Message: Cannot add or update a child row: a foreign key constraint fails

How To Ask Questions How To Answer Questions
Jimmy Leo

Joined: Apr 30, 2011
Posts: 2

Follow this link for a hastable that maps between MySQL error and SQLState

I believe you can get the error message using these two lines of codes:
String stateCode = SQLError.mysqlToXOpen(SQLExceptionObject.getErrorCode());
String MySQL_Error= SQLError.get(stateCode);

Hope this help.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Welcome to the Ranch

You might be a bit late, after a month, however.
Jimmy Leo

Joined: Apr 30, 2011
Posts: 2
Campbell Ritchie wrote:Welcome to the Ranch

You might be a bit late, after a month, however.


Yeah I am working with JSP using MySQL too and was looking something similar to mysql_error() in PHP
and found this site and the link I posted.

Anyways, hope it helps others and a method much more like mysql_error() can be developed
I agree. Here's the link:
subject: Mysql Error Messages in Java
It's not a secret anymore!