aspose file tools
The moose likes JDBC and Relational Databases and the fly likes How to throw an Exception from SQL code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Storm Applied this week in the Other Open Source APIs forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to throw an Exception from SQL code" Watch "How to throw an Exception from SQL code" New topic
Author

How to throw an Exception from SQL code

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42020
    
  31
I need a table called "asset" which contains assets, would you believe. Now it is possible to buy an asset, so I wrote a stored procedure, and I can easily write a stored procedure for disposing of an asset (eg sold, damaged, stolen). MySQL 5.0.51 or thereabouts.

But I don't want to call the dispose_of_asset procedure twice on the same asset. Is there any way to write SQL code which will raise an SQLException on JDBC if the dispose_of_asset procedure is called on something which has already been disposed of?

I have found this by Googling, which said to select a non-existent column. Is there a more elegant way to do it.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19469
    
  10

Do you have to have the stored procedure throw an exception, or could you have it return one value if it did its work correctly and a different value if it failed?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42020
    
  31
It isn't actually returning a value at present, so I could return TRUE or FALSE and use ROLLBACK; if the asset has already been disposed of.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42020
    
  31
Thank you. I shall try changing it to a function with a boolean return type. Then whoever implements the JDBC bit (myself) will know to watch for that return.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to throw an Exception from SQL code