0

I made a java application that read a text file and store it in a database.The database has 2 tables So in order to store the value of text i created two prepared statement in this order

  • code for storing data in table 1
  • code for storing data in table 2

And that worked perfectly. However, when there is a Semantic Errors in the second table , the data in the first are still stored , which is not what i intended to.

In a nut shell , if there any semantic error int the second table i want nothing to be stored in the database. So how can i do it ?

Andreas Dolk
115k20 gold badges185 silver badges275 bronze badges
asked Aug 16, 2011 at 21:32
3
  • 3
    You need to think atomically, or transactionally :-) Commented Aug 16, 2011 at 21:34
  • download.oracle.com/javase/tutorial/jdbc/basics/… Commented Aug 16, 2011 at 21:35
  • i tried to empty the stored line back in the code for the second table but it won't work. I didn't understand your answer Commented Aug 16, 2011 at 21:36

4 Answers 4

3

It may depend on your database, but you should use a transaction, catch any exceptions and rollback the transaction in the case where the data is bad.

This link might help you

answered Aug 16, 2011 at 21:35
Sign up to request clarification or add additional context in comments.

Comments

2

What you want is a transaction in your database, which will allow you to roll back the first insert if something goes wrong with the second one.

You're not specifying which database connection technology you're using, but whichever it is try looking through its documentation for "transaction" support.

answered Aug 16, 2011 at 21:35

Comments

1

You need to use a transaction. Set autocommit on the connection to false, execute the 2 statements. If there is an exception then catch this and rollback the transaction, otherwise commit. Thanks

answered Aug 16, 2011 at 21:36

Comments

0

Verify the inputs before writing them to the database.

answered Aug 16, 2011 at 21:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.