0

I have a situation in java where in:

while(resultset.next()){
   try{
      //some executable prepared statements written
   }
   catch(MySQLIntegrityConstraintViolationException ex){
      //block2
   }
   catch(SQLException ex){
      //block1
   }
}

now here , what I am trying to do is that if an MySQLIntegrityConstraintViolationException is seen, I have to skip it and just continue with the next record... I am unable to find a mechanism to do this. Please could someone suggest what should be done so that the exception is ignored.

Please let me know..Thank you very much in advance.

2
  • 4
    This is your 16th question, and you're still not formatting code? Read the How to Format box to the right of where you typed your question, and the [?] link above the text box you were typing in. It's time to fly solo rather than having other people clean up for you. Commented Nov 29, 2010 at 17:46
  • @TJ, I formatted it, it was too hard on my eyes :) Commented Nov 29, 2010 at 17:49

2 Answers 2

1

Understand your database better.

There are lots of issues here.

1) does your prepared statement participate in a transaction? (i.e. does it rollback?)
2) does the prepared statement update rows?
3) can you guarantee that the database is in a known state after the exception is caught and handled?

Trying to ignore the exception is the wrong approach. The current code (as shown) does let you repeat the prepared statement after the exception is caught. 'MySQLIntegrityConstraintViolationException' tells me that the database could not do something you asked it to; maybe a duplicate key or a delete involving a required foreign key.

You need to understand the constraints of the database tables to really know how to should respond to this exception.

In the case of inserting duplicates, I've had situations where I had to tell the db engine to ignore dups, or to indicate a table/file dups should be piped to.

The best answer here is to understand why your prepared statement is throwing the exception and then plan how to respond to the situation. If you already know this, please add it to the question, otherwise we'll be grabbing at straws.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you might be getting any other exception , try doing following to find out if you are getting any other exception,

while(resultset.next()){
   try{
  //some executable prepared statements written
   }
   catch(Exception ex){
   if(ex instanceof MySQLIntegrityConstraintViolationException)
   continue;
   else
   ex.printStackTrace();
  }

}

2 Comments

There is a problem with the preparedStatemet.execute()...And, I am trying to execute that statement again and again..which is not happening as the flow goes to the catch block..what to do ??Please help
yes, by ex.printStackTrace(); you will get to know, where is the problem and which exception are you getting...and then go forward from there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.