• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete verification addon in my jframe form  RSS feed

 
Farhan Karim
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a basic CRUD application in Java. I have a JFrame form with add, delete, edit and update options. I have designed the delete button with just the ability to search by user ID and delete the entire record from table, but I am confused on what to do if the ID doesn't exist or the ID is already deleted. I get the same message in all three scenarios in my try catch block. Can someone help me design errors to show when the user ID doesn't exist and when the user ID is already deleted?

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can tell if the SQL statement actually deleted any data by checking the return value of the execute() call and then calling the appropriate getXXX method to get the resultSet or update count. For an SQL delete statement execute should return false and getUpdateCount() should return the number of rows that were updated. Alternatively you could call executeUpdate() instead of execute() which directly returns the number of rows effected by the operation.

BTW You can't distinguish between doesn't exist and already deleted unless your application/database keeps a record of used ID's or deleted ID's or, if the ID's are contiguous, the current maximum ID.
 
Farhan Karim
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you provide an example ??
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW I should add that prepared statements are normally used where you prepare the statement in advance inserting '?' where you want to add values at a later date. You can then reuse the prepared statement each time you want to execute that particular SQL statement. See this tutorial: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html.

Also without seeing all your code it's hard to be sure but I suspect you aren't closing the statement and/or connection on all execution paths after you have finished with them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!