File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mysql Error Messages in Java

 
Jose Dobles
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jimmy Leo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You might be a bit late, after a month, however.
 
Jimmy Leo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic