• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mysql Error Messages in Java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You might be a bit late, after a month, however.
 
Jimmy Leo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic