aspose file tools*
The moose likes JDBC 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
Bookmark "Mysql Error Messages in Java" Watch "Mysql Error Messages in Java" New topic
Author

Mysql Error Messages in Java

Jose Dobles
Greenhorn

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: 1125

Jose,
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,
Avi.
Paul Sturrock
Bartender

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
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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,
Error: 1216 SQLSTATE: 23000 (ER_NO_REFERENCED_ROW)

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jimmy Leo
Greenhorn

Joined: Apr 30, 2011
Posts: 2
http://www.java2s.com/Code/Java/Database-SQL-JDBC/MySQLErrorcodeandmessage.htm

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
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Welcome to the Ranch

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

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

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


Thanks!

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
 
wood burning stoves
 
subject: Mysql Error Messages in Java