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
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?
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.
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.
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
Verify the inputs before writing them to the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.