Forums Register Login

Mysql Error Messages in Java

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Welcome to the Ranch

You might be a bit late, after a month, however.
+Pie Number of slices to send: Send
 

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
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5816 times.
Similar Threads
data grid with edit and delete options
JTable Row Deletion
Update Table Nested Query (Using Function)
Adding 1 row to specific section of a struts table using iterate tag
maintaining session
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:05:23.